aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/lodmod/renderer/GPUMemoryManager.java
blob: ebfb3347ffea34242bf67b8c841712155ea47b26 (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
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 moved = 0;
        int checksLeft = sentMeshes.size();
        
        while(moved < 1 && checksLeft-- > 0 && !sentMeshes.isEmpty()) {
            nextMesh++;
            if(nextMesh >= sentMeshes.size()) {
                nextMesh = 0;
            }
            Mesh mesh = sentMeshes.get(nextMesh);
            
            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;
            } else if(mesh.gpuStatus == GPUStatus.PENDING_DELETE) {
                mesh.iFirst = 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 int malloc(int size) {
        int nextBase = 0;
        if(!sentMeshes.isEmpty()) {
            if(nextMesh < sentMeshes.size() - 1) {
                Mesh next = sentMeshes.get(nextMesh);
                Mesh nextnext = sentMeshes.get(nextMesh + 1);
                if(nextnext.offset - next.getEnd() >= size) {
                    return next.getEnd();
                }
            }
            
            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 size = mesh.bufferSize();
        int insertIndex = -1;
        
        int 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(nextBase + size >= BUFFER_SIZE) {
            return;
        }
        
        
        
        if(mesh.gpuStatus == GPUStatus.UNSENT) {
            mesh.prepareBuffer();
            
            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            
            glBufferSubData(GL_ARRAY_BUFFER, nextBase, mesh.buffer);
            mesh.iFirst = nextBase / mesh.getStride();
            mesh.iCount = mesh.quadCount * 6;
            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() {
        return Arrays.asList("VRAM: " + ((sentMeshes.isEmpty() ? 0 : sentMeshes.get(sentMeshes.size() - 1).getEnd()) / 1024 / 1024) + "MB / " + (BUFFER_SIZE / 1024 / 1024) + "MB");
    }

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