-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworld_renderer.d
282 lines (254 loc) · 7.38 KB
/
world_renderer.d
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
module dreams.world_renderer;
import core.thread;
import dreams.culling, dreams.view, dreams.world, dreams.world_mesh;
import std.datetime.stopwatch: StopWatch;
import std.concurrency, std.exception, std.math;
import log, matrix, renderer, vector;
struct WorldShader
{
Shader shader;
ShaderUniform mvp;
ShaderUniform texture;
void init(Renderer renderer)
{
shader = renderer.findShader("world");
mvp = renderer.getShaderUniform(shader, "mvp");
texture = renderer.getShaderUniform(shader, "texture");
}
}
enum mesherThreadsNum = 8; // TODO: configurable var
final class WorldRenderer
{
private Renderer renderer;
private WorldShader worldShader;
public Texture worldTexture; //public to show the tiles in the editor
private Tid[] mesherThreads;
private uint nextMesher; // index of the mesher thread to use (to enable workload sharing)
private WorldNode* root;
private Vec3f position; // viewer position
private Mat4f vp; // view-projection matrix
private RadarTest frustumCulling;
private WorldChunk*[] chunksToBeRendered;
enum size_t maxChunkMeshMem = 64 * 2 ^^ 20;
size_t chunkMeshMem;
int chunkMeshCount;
WorldChunk* first, last; // list of chunks with an allocated mesh
this(Renderer renderer)
{
assert(renderer);
this.renderer = renderer;
chunksToBeRendered.reserve(512);
}
void init()
{
worldShader.init(renderer);
worldTexture = renderer.loadTexture("world.png", TextureFilter.atlas, TextureWrap.clamp);
}
void shutdown()
{
asyncStopMesherThreads();
destroyAllMeshes();
renderer.destroyTexture(worldTexture);
}
void setWorldRoot(WorldNode* root)
{
if (this.root != root) {
asyncStopMesherThreads();
this.root = root;
destroyAllMeshes();
mesherThreads.length = 0;
assumeSafeAppend(mesherThreads);
for (int i = 0; i < mesherThreadsNum; i++) {
mesherThreads ~= spawn(&mesherThread, cast(immutable WorldNode*) root);
}
}
}
void setView(const ref View view)
{
this.position = view.position;
this.vp = view.vp;
with (view) {
frustumCulling.set(position, forward, side, up, aspect, near, far, fov);
}
}
void render()
{
StopWatch sw;
sw.start();
while (receiveTimeout(msecs(-1), &onChunkMeshReady)) {
if (sw.peek() >= msecs(5)) {
break;
}
}
chunksToBeRendered.length = 0;
assumeSafeAppend(chunksToBeRendered);
findVisibleChunks(root);
drawChunks();
while (chunkMeshMem > maxChunkMeshMem) {
chunkMeshMem -= first.meshSize;
chunkMeshCount--;
first.needUpdate = true;
first.meshSize = 0;
renderer.destroyVertexBuffer(first.vertexBuffer);
renderer.destroyIndexBuffer(first.indexBuffer);
first.indexBuffer = IndexBuffer.init;
first.vertexBuffer = VertexBuffer.init;
//dev("Chunk %s mesh deleted", first.position.array);
first.next.prev = null;
WorldChunk* next = first.next;
first.prev = first.next = null;
first = next;
}
}
void destroyAllMeshes()
{
WorldChunk* chunk = first;
while (chunk) {
chunk.needUpdate = true;
chunk.waitingMesher = false;
chunk.meshSize = 0;
renderer.destroyIndexBuffer(chunk.indexBuffer);
renderer.destroyVertexBuffer(chunk.vertexBuffer);
chunk.indexBuffer = IndexBuffer.init;
chunk.vertexBuffer = VertexBuffer.init;
WorldChunk* next = chunk.next;
chunk.prev = chunk.next = null; // remove from the list
chunk = next;
}
chunkMeshMem = 0;
chunkMeshCount = 0;
}
private void findVisibleChunks(WorldNode* node)
{
immutable float sqrt3 = 1.73205080f;
if (frustumCulling.sphereTest(node.center, node.getExtent() * sqrt3)) {
if (node.level == 1) {
foreach (chunk; node.chunks) {
if (chunk) {
immutable adjust = Vec3f(chunkSize / 2, chunkSize / 2, chunkSize / 2);
Vec3f chunkCenter = chunk.position + adjust;
immutable float chunkRadius = chunkSize / 2 * sqrt3;
if (frustumCulling.sphereTest(chunkCenter, chunkRadius)) {
addVisibleChunk(chunk);
}
}
}
} else {
foreach (child; node.nodes) {
if (child) findVisibleChunks(child);
}
}
}
}
private void addVisibleChunk(WorldChunk* chunk)
{
if ((chunk.indexBuffer == 0 || chunk.needUpdate == true) && !chunk.waitingMesher) {
chunk.waitingMesher = true;
asyncCreateChunkMesh(
chunk,
cast(uint) chunk.position.x,
cast(uint) chunk.position.y,
cast(uint) chunk.position.z
);
}
if (chunk.indexBuffer != 0) {
chunksToBeRendered ~= chunk;
}
}
private void drawChunks()
{
Mat4f mvp;
renderer.setState(RenderState.cullFace | RenderState.depthTest);
renderer.setShader(worldShader.shader);
renderer.setTexture(worldTexture);
worldShader.texture.setInteger(0);
foreach (chunk; chunksToBeRendered) {
auto translation = translationMatrix(
chunk.position.x,
chunk.position.y,
chunk.position.z
);
mvp = vp * translation;
worldShader.mvp.setMat4f(mvp);
renderer.draw(chunk.indexBuffer, chunk.indexCount, chunk.vertexBuffer);
}
}
private void asyncCreateChunkMesh(WorldChunk* chunk, uint x, uint y, uint z)
{
send(mesherThreads[nextMesher++], cast(immutable WorldChunk*) chunk, x, y, z);
//dev("asyncCreateChunkMesh %s", chunk.position.array);
if (nextMesher == mesherThreads.length) {
nextMesher = 0;
}
}
private void asyncStopMesherThreads()
{
foreach (tid; mesherThreads) {
prioritySend(tid, 0);
}
}
private void onChunkMeshReady(immutable(WorldChunk)* chunk_, immutable(WorldMesh)* mesh_, Tid tid)
{
//dev("onChunkMeshReady %s", chunk_.position.array);
WorldChunk* chunk = cast(WorldChunk*) chunk_;
WorldMesh* mesh = cast(WorldMesh*) mesh_;
// TODO: should check if asyncStopMesherThreads has been called ...
if (!(chunk.prev || chunk.next)) {
// add the chunk to the list
if (!first) { // list empty
first = last = chunk;
} else {
last.next = chunk;
chunk.prev = last;
last = chunk;
}
chunkMeshCount++;
} else {
// move the chunk to the last position in the list
if (last != chunk) {
if (chunk.prev) chunk.prev.next = chunk.next;
else first = chunk.next;
chunk.next.prev = chunk.prev;
last.next = chunk;
chunk.prev = last;
chunk.next = null;
last = chunk;
}
}
chunkMeshMem -= chunk.meshSize;
chunk.needUpdate = false;
chunk.waitingMesher = false;
if (!chunk.indexBuffer) chunk.indexBuffer = renderer.createIndexBuffer();
if (!chunk.vertexBuffer) chunk.vertexBuffer = renderer.createVertexBuffer();
renderer.updateIndexBuffer(chunk.indexBuffer, mesh.indices, BufferUsage.staticDraw);
chunk.indexCount = cast(int) mesh.indices.length;
renderer.updateVertexBuffer(chunk.vertexBuffer, mesh.vertices, BufferUsage.staticDraw);
chunk.meshSize = mesh.indices.length * Index.sizeof + mesh.vertices.length * Vertex.sizeof;
send(tid, 0); // unlock the mesher
chunkMeshMem += chunk.meshSize;
//dev("Chunk %s mesh updated", chunk.position.array);
}
}
/*
Mesher thread
*/
private void mesherThread(immutable(WorldNode)* root)
{
WorldMesh worldMesh;
worldMesh.reserve(2048);
worldMesh.setWorldRoot(root);
void createMesh(immutable(WorldChunk)* chunk, uint x, uint y, uint z) {
worldMesh.create(chunk, x, y, z);
//dev("mesherThread send %s", chunk.position.array);
send(ownerTid(), chunk, cast(immutable WorldMesh*) &worldMesh, thisTid());
receive((int i) {}); // wait till the renderer thread has processed the mesh
}
bool running = true;
while (running) {
receive(
&createMesh,
(int) { running = false; },
(OwnerTerminated e) { running = false; }
);
}
}