aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/neodymium/renderer/GPUMemoryManager.java
blob: 6f5e4fe83f21458c267ad86fb79a00726b216b4f (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package makamys.neodymium.renderer;

import static org.lwjgl.opengl.GL15.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import makamys.neodymium.Compat;
import makamys.neodymium.Neodymium;
import makamys.neodymium.config.Config;
import makamys.neodymium.renderer.Mesh.GPUStatus;
import makamys.neodymium.util.GuiHelper;
import makamys.neodymium.util.ChatUtil;

import static makamys.neodymium.Constants.LOGGER;

/** Manages dynamic memory allocation inside a fixed buffer on the GPU. */
public class GPUMemoryManager {
    
    private long bufferSize;
    
    public int VBO;
    
    private int nextMesh;
    
    private List<Mesh> sentMeshes = new ArrayList<>();
    
    private long usedVRAM;
    private long lastUsedVRAMUpdate;
    private static final long USED_VRAM_UPDATE_RATE = 1_000_000_000L;
    
    private static final long MAX_VRAM_FULLNESS_INTERVAL = 30L * 1_000_000_000L;
    private static long lastVRAMFullness = -1;
    
    public GPUMemoryManager() {
        VBO = glGenBuffers();
        
        bufferSize = ((long)Config.VRAMSize) * 1024 * 1024;
        
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        
        glBufferData(GL_ARRAY_BUFFER, bufferSize, GL_DYNAMIC_DRAW);
        
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    
    public void runGC(boolean full) {
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        
        int moved = 0;
        int timesReachedEnd = 0;
        int checksLeft = sentMeshes.size();
        
        final int MB256 = 256 * 1024 * 1024;
        
        int panicRate = (int)(((float)Math.max(MB256 - (bufferSize - end()), 0) / (float)MB256) * 64f);
        
        while((!full && (moved < (4 + panicRate) && checksLeft-- > 0)) || (full && timesReachedEnd < 2) && !sentMeshes.isEmpty()) {
            nextMesh++;
            if(nextMesh >= sentMeshes.size()) {
                nextMesh = 0;
                timesReachedEnd++;
            }
            Mesh mesh = sentMeshes.get(nextMesh);
            
            if(mesh.gpuStatus == GPUStatus.SENT) {
                long offset = nextMesh == 0 ? 0 : sentMeshes.get(nextMesh - 1).getEnd();
                if(mesh.offset != offset) {
                    glBufferSubData(GL_ARRAY_BUFFER, offset, mesh.buffer);
                    moved++;
                }
                mesh.iFirst = (int)(offset / Neodymium.renderer.getStride());
                mesh.offset = offset;
            } else if(mesh.gpuStatus == GPUStatus.PENDING_DELETE) {
                mesh.iFirst = -1;
                mesh.offset = -1;
                mesh.visible = false;
                mesh.gpuStatus = GPUStatus.UNSENT;
                
                sentMeshes.remove(nextMesh);
                
                mesh.destroyBuffer();
                
                if(nextMesh > 0) {
                    nextMesh--;
                }
            }
        }
        
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    
    private long end() {
        return (sentMeshes.isEmpty() ? 0 : sentMeshes.get(sentMeshes.size() - 1).getEnd());
    }
    
    public void sendMeshToGPU(Mesh mesh) {
        if(mesh == null || mesh.buffer == null) {
            return;
        }
        
        if(end() + mesh.bufferSize() >= bufferSize) {
            runGC(true);
        }
        
        if(end() + mesh.bufferSize() >= bufferSize) {
            Neodymium.renderer.destroyPending = true;
            
            long t = System.nanoTime();
            
            if(lastVRAMFullness != -1 && t - lastVRAMFullness < MAX_VRAM_FULLNESS_INTERVAL) {
                ChatUtil.showNeoChatMessage("VRAM keeps getting full! Reverting to vanilla renderer. Try increasing the VRAM buffer size in the config, if possible.", ChatUtil.MessageVerbosity.ERROR, false);
                Compat.onNotEnoughVRAM(Config.VRAMSize);
            } else {
                LOGGER.debug("Reloading renderer because VRAM is full.");
                // TODO restart renderer with more VRAM allocated when this happens.
            }
            lastVRAMFullness = t;
            return;
        }
        
        int size = mesh.bufferSize();
        int insertIndex = -1;
        
        long nextBase = -1;
        if(!sentMeshes.isEmpty()) {
            if(nextMesh < sentMeshes.size() - 1) {
                Mesh next = sentMeshes.get(nextMesh);
                Mesh nextnext = null;
                for(int i = nextMesh + 1; i < sentMeshes.size(); i++) {
                    Mesh m = sentMeshes.get(i);
                    if(m.gpuStatus == Mesh.GPUStatus.SENT) {
                        nextnext = m;
                        break;
                    }
                }
                if(nextnext != null && nextnext.offset - next.getEnd() >= size) {
                    nextBase = next.getEnd();
                    insertIndex = nextMesh + 1;
                }
            }
            
            if(nextBase == -1) {
                nextBase = sentMeshes.get(sentMeshes.size() - 1).getEnd();
            }
        }
        if(nextBase == -1) nextBase = 0;
        
        
        if(mesh.gpuStatus == GPUStatus.UNSENT) {
            mesh.prepareBuffer();
            
            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            
            glBufferSubData(GL_ARRAY_BUFFER, nextBase, mesh.buffer);
            mesh.iFirst = (int)(nextBase / Neodymium.renderer.getStride());
            mesh.iCount = mesh.quadCount * 4;
            mesh.offset = nextBase;
            
            if(insertIndex == -1) {
                sentMeshes.add(mesh);
            } else {
                sentMeshes.add(insertIndex, mesh);
                nextMesh = insertIndex;
            }
            
            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() {
        long t = System.nanoTime();
        if(t - lastUsedVRAMUpdate > USED_VRAM_UPDATE_RATE) {
            usedVRAM = 0;
            for(Mesh mesh : sentMeshes) {
                usedVRAM += mesh.bufferSize();
            }
            lastUsedVRAMUpdate = t;
        }
        return Arrays.asList("VRAM: " + (usedVRAM / 1024 / 1024) + "MB (" + (end() / 1024 / 1024) + "MB) / " + (bufferSize / 1024 / 1024) + "MB");
    }

    public void drawInfo() {
        int scale = 10000;
        int rowLength = 512;
        int yOff = 20;
        
        int height = (int)(bufferSize / scale) / rowLength;
        GuiHelper.drawRectangle(0, yOff, rowLength, height, 0x000000, 50);
        
        int meshI = 0;
        for(Mesh mesh : sentMeshes) {
            
            int o = (int)(mesh.offset / 10000);
            int o2 = (int)((mesh.offset + mesh.bufferSize()) / 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((int)(bufferSize / scale) % rowLength, (int)(bufferSize / scale) / rowLength + yOff, 4, 4, 0xFF0000);
    }
    
    public float getCoherenceRate() {
        return (float)ChunkMesh.usedRAM / (float)end();
    }
    
}