]> git.lizzy.rs Git - minetest.git/blob - src/client/clientmap.cpp
8x block meshes (#13133)
[minetest.git] / src / client / clientmap.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "clientmap.h"
21 #include "client.h"
22 #include "mapblock_mesh.h"
23 #include <IMaterialRenderer.h>
24 #include <matrix4.h>
25 #include "mapsector.h"
26 #include "mapblock.h"
27 #include "profiler.h"
28 #include "settings.h"
29 #include "camera.h"               // CameraModes
30 #include "util/basic_macros.h"
31 #include "client/renderingengine.h"
32
33 #include <queue>
34
35 // struct MeshBufListList
36 void MeshBufListList::clear()
37 {
38         for (auto &list : lists)
39                 list.clear();
40 }
41
42 void MeshBufListList::add(scene::IMeshBuffer *buf, v3s16 position, u8 layer)
43 {
44         // Append to the correct layer
45         std::vector<MeshBufList> &list = lists[layer];
46         const video::SMaterial &m = buf->getMaterial();
47         for (MeshBufList &l : list) {
48                 // comparing a full material is quite expensive so we don't do it if
49                 // not even first texture is equal
50                 if (l.m.TextureLayer[0].Texture != m.TextureLayer[0].Texture)
51                         continue;
52
53                 if (l.m == m) {
54                         l.bufs.emplace_back(position, buf);
55                         return;
56                 }
57         }
58         MeshBufList l;
59         l.m = m;
60         l.bufs.emplace_back(position, buf);
61         list.emplace_back(l);
62 }
63
64 static void on_settings_changed(const std::string &name, void *data)
65 {
66         static_cast<ClientMap*>(data)->onSettingChanged(name);
67 }
68 // ClientMap
69
70 ClientMap::ClientMap(
71                 Client *client,
72                 RenderingEngine *rendering_engine,
73                 MapDrawControl &control,
74                 s32 id
75 ):
76         Map(client),
77         scene::ISceneNode(rendering_engine->get_scene_manager()->getRootSceneNode(),
78                 rendering_engine->get_scene_manager(), id),
79         m_client(client),
80         m_rendering_engine(rendering_engine),
81         m_control(control),
82         m_drawlist(MapBlockComparer(v3s16(0,0,0)))
83 {
84
85         /*
86          * @Liso: Sadly C++ doesn't have introspection, so the only way we have to know
87          * the class is whith a name ;) Name property cames from ISceneNode base class.
88          */
89         Name = "ClientMap";
90         m_box = aabb3f(-BS*1000000,-BS*1000000,-BS*1000000,
91                         BS*1000000,BS*1000000,BS*1000000);
92
93         /* TODO: Add a callback function so these can be updated when a setting
94          *       changes.  At this point in time it doesn't matter (e.g. /set
95          *       is documented to change server settings only)
96          *
97          * TODO: Local caching of settings is not optimal and should at some stage
98          *       be updated to use a global settings object for getting thse values
99          *       (as opposed to the this local caching). This can be addressed in
100          *       a later release.
101          */
102         m_cache_trilinear_filter  = g_settings->getBool("trilinear_filter");
103         m_cache_bilinear_filter   = g_settings->getBool("bilinear_filter");
104         m_cache_anistropic_filter = g_settings->getBool("anisotropic_filter");
105         m_cache_transparency_sorting_distance = g_settings->getU16("transparency_sorting_distance");
106         m_enable_raytraced_culling = g_settings->getBool("enable_raytraced_culling");
107         g_settings->registerChangedCallback("enable_raytraced_culling", on_settings_changed, this);
108 }
109
110 void ClientMap::onSettingChanged(const std::string &name)
111 {
112         if (name == "enable_raytraced_culling")
113                 m_enable_raytraced_culling = g_settings->getBool("enable_raytraced_culling");
114 }
115
116 ClientMap::~ClientMap()
117 {
118         g_settings->deregisterChangedCallback("enable_raytraced_culling", on_settings_changed, this);
119 }
120
121 void ClientMap::updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset)
122 {
123         v3s16 previous_node = floatToInt(m_camera_position, BS) + m_camera_offset;
124         v3s16 previous_block = getContainerPos(previous_node, MAP_BLOCKSIZE);
125
126         m_camera_position = pos;
127         m_camera_direction = dir;
128         m_camera_fov = fov;
129         m_camera_offset = offset;
130
131         v3s16 current_node = floatToInt(m_camera_position, BS) + m_camera_offset;
132         v3s16 current_block = getContainerPos(current_node, MAP_BLOCKSIZE);
133
134         // reorder the blocks when camera crosses block boundary
135         if (previous_block != current_block)
136                 m_needs_update_drawlist = true;
137
138         // reorder transparent meshes when camera crosses node boundary
139         if (previous_node != current_node)
140                 m_needs_update_transparent_meshes = true;
141 }
142
143 MapSector * ClientMap::emergeSector(v2s16 p2d)
144 {
145         // Check that it doesn't exist already
146         MapSector *sector = getSectorNoGenerate(p2d);
147
148         // Create it if it does not exist yet
149         if (!sector) {
150                 sector = new MapSector(this, p2d, m_gamedef);
151                 m_sectors[p2d] = sector;
152         }
153
154         return sector;
155 }
156
157 void ClientMap::OnRegisterSceneNode()
158 {
159         if(IsVisible)
160         {
161                 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
162                 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
163         }
164
165         ISceneNode::OnRegisterSceneNode();
166         // It's not needed to register this node to the shadow renderer
167         // we have other way to find it
168 }
169
170 void ClientMap::getBlocksInViewRange(v3s16 cam_pos_nodes,
171                 v3s16 *p_blocks_min, v3s16 *p_blocks_max, float range)
172 {
173         if (range <= 0.0f)
174                 range = m_control.wanted_range;
175
176         v3s16 box_nodes_d = range * v3s16(1, 1, 1);
177         // Define p_nodes_min/max as v3s32 because 'cam_pos_nodes -/+ box_nodes_d'
178         // can exceed the range of v3s16 when a large view range is used near the
179         // world edges.
180         v3s32 p_nodes_min(
181                 cam_pos_nodes.X - box_nodes_d.X,
182                 cam_pos_nodes.Y - box_nodes_d.Y,
183                 cam_pos_nodes.Z - box_nodes_d.Z);
184         v3s32 p_nodes_max(
185                 cam_pos_nodes.X + box_nodes_d.X,
186                 cam_pos_nodes.Y + box_nodes_d.Y,
187                 cam_pos_nodes.Z + box_nodes_d.Z);
188         // Take a fair amount as we will be dropping more out later
189         // Umm... these additions are a bit strange but they are needed.
190         *p_blocks_min = v3s16(
191                         p_nodes_min.X / MAP_BLOCKSIZE - 3,
192                         p_nodes_min.Y / MAP_BLOCKSIZE - 3,
193                         p_nodes_min.Z / MAP_BLOCKSIZE - 3);
194         *p_blocks_max = v3s16(
195                         p_nodes_max.X / MAP_BLOCKSIZE + 1,
196                         p_nodes_max.Y / MAP_BLOCKSIZE + 1,
197                         p_nodes_max.Z / MAP_BLOCKSIZE + 1);
198 }
199
200 class MapBlockFlags
201 {
202 public:
203         static constexpr u16 CHUNK_EDGE = 8;
204         static constexpr u16 CHUNK_MASK = CHUNK_EDGE - 1;
205         static constexpr std::size_t CHUNK_VOLUME = CHUNK_EDGE * CHUNK_EDGE * CHUNK_EDGE; // volume of a chunk
206
207         MapBlockFlags(v3s16 min_pos, v3s16 max_pos)
208                         : min_pos(min_pos), volume((max_pos - min_pos) / CHUNK_EDGE + 1)
209         {
210                 chunks.resize(volume.X * volume.Y * volume.Z);
211         }
212
213         class Chunk
214         {
215         public:
216                 inline u8 &getBits(v3s16 pos)
217                 {
218                         std::size_t address = getAddress(pos);
219                         return bits[address];
220                 }
221
222         private:
223                 inline std::size_t getAddress(v3s16 pos) {
224                         std::size_t address = (pos.X & CHUNK_MASK) + (pos.Y & CHUNK_MASK) * CHUNK_EDGE + (pos.Z & CHUNK_MASK) * (CHUNK_EDGE * CHUNK_EDGE);
225                         return address;
226                 }
227
228                 std::array<u8, CHUNK_VOLUME> bits;
229         };
230
231         Chunk &getChunk(v3s16 pos)
232         {
233                 v3s16 delta = (pos - min_pos) / CHUNK_EDGE;
234                 std::size_t address = delta.X + delta.Y * volume.X + delta.Z * volume.X * volume.Y;
235                 Chunk *chunk = chunks[address].get();
236                 if (!chunk) {
237                         chunk = new Chunk();
238                         chunks[address].reset(chunk);
239                 }
240                 return *chunk;
241         }
242 private:
243         std::vector<std::unique_ptr<Chunk>> chunks;
244         v3s16 min_pos;
245         v3s16 volume;
246 };
247
248 void ClientMap::updateDrawList()
249 {
250         ScopeProfiler sp(g_profiler, "CM::updateDrawList()", SPT_AVG);
251
252         m_needs_update_drawlist = false;
253
254         for (auto &i : m_drawlist) {
255                 MapBlock *block = i.second;
256                 block->refDrop();
257         }
258         m_drawlist.clear();
259
260         v3s16 cam_pos_nodes = floatToInt(m_camera_position, BS);
261
262         v3s16 p_blocks_min;
263         v3s16 p_blocks_max;
264         getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
265
266         // Number of blocks occlusion culled
267         u32 blocks_occlusion_culled = 0;
268         // Blocks visited by the algorithm
269         u32 blocks_visited = 0;
270         // Block sides that were not traversed
271         u32 sides_skipped = 0;
272
273         // No occlusion culling when free_move is on and camera is inside ground
274         bool occlusion_culling_enabled = true;
275         if (m_control.allow_noclip) {
276                 MapNode n = getNode(cam_pos_nodes);
277                 if (n.getContent() == CONTENT_IGNORE || m_nodedef->get(n).solidness == 2)
278                         occlusion_culling_enabled = false;
279         }
280
281         v3s16 camera_block = getContainerPos(cam_pos_nodes, MAP_BLOCKSIZE);
282         m_drawlist = std::map<v3s16, MapBlock*, MapBlockComparer>(MapBlockComparer(camera_block));
283
284         auto is_frustum_culled = m_client->getCamera()->getFrustumCuller();
285
286         // Uncomment to debug occluded blocks in the wireframe mode
287         // TODO: Include this as a flag for an extended debugging setting
288         // if (occlusion_culling_enabled && m_control.show_wireframe)
289         //      occlusion_culling_enabled = porting::getTimeS() & 1;
290
291         std::queue<v3s16> blocks_to_consider;
292
293         // Bits per block:
294         // [ visited | 0 | 0 | 0 | 0 | Z visible | Y visible | X visible ]
295         MapBlockFlags blocks_seen(p_blocks_min, p_blocks_max);
296
297         // Start breadth-first search with the block the camera is in
298         blocks_to_consider.push(camera_block);
299         blocks_seen.getChunk(camera_block).getBits(camera_block) = 0x07; // mark all sides as visible
300
301         std::set<v3s16> shortlist;
302
303         // Recursively walk the space and pick mapblocks for drawing
304         while (blocks_to_consider.size() > 0) {
305
306                 v3s16 block_coord = blocks_to_consider.front();
307                 blocks_to_consider.pop();
308
309                 auto &flags = blocks_seen.getChunk(block_coord).getBits(block_coord);
310
311                 // Only visit each block once (it may have been queued up to three times)
312                 if ((flags & 0x80) == 0x80)
313                         continue;
314                 flags |= 0x80;
315
316                 blocks_visited++;
317
318                 // Get the sector, block and mesh
319                 MapSector *sector = this->getSectorNoGenerate(v2s16(block_coord.X, block_coord.Z));
320
321                 if (!sector)
322                         continue;
323
324                 MapBlock *block = sector->getBlockNoCreateNoEx(block_coord.Y);
325
326                 MapBlockMesh *mesh = block ? block->mesh : nullptr;
327
328
329                 // Calculate the coordinates for range and frutum culling
330                 v3f mesh_sphere_center;
331                 f32 mesh_sphere_radius;
332
333                 v3s16 block_pos_nodes = block_coord * MAP_BLOCKSIZE;
334
335                 if (mesh) {
336                         mesh_sphere_center = intToFloat(block_pos_nodes, BS)
337                                         + mesh->getBoundingSphereCenter();
338                         mesh_sphere_radius = mesh->getBoundingRadius();
339                 }
340                 else {
341                         mesh_sphere_center = intToFloat(block_pos_nodes, BS) + v3f((MAP_BLOCKSIZE * 0.5f - 0.5f) * BS);
342                         mesh_sphere_radius = 0.0f;
343                 }
344
345                 // First, perform a simple distance check.
346                 if (!m_control.range_all &&
347                         mesh_sphere_center.getDistanceFrom(intToFloat(cam_pos_nodes, BS)) >
348                                 m_control.wanted_range * BS + mesh_sphere_radius)
349                         continue; // Out of range, skip.
350
351                 // Frustum culling
352                 // Only do coarse culling here, to account for fast camera movement.
353                 // This is needed because this function is not called every frame.
354                 float frustum_cull_extra_radius = 300.0f;
355                 if (is_frustum_culled(mesh_sphere_center,
356                                 mesh_sphere_radius + frustum_cull_extra_radius))
357                         continue;
358
359                 // Calculate the vector from the camera block to the current block
360                 // We use it to determine through which sides of the current block we can continue the search
361                 v3s16 look = block_coord - camera_block;
362
363                 // Occluded near sides will further occlude the far sides
364                 u8 visible_outer_sides = flags & 0x07;
365
366                 // Raytraced occlusion culling - send rays from the camera to the block's corners
367                 if (occlusion_culling_enabled && m_enable_raytraced_culling &&
368                                 block && mesh &&
369                                 visible_outer_sides != 0x07 && isBlockOccluded(block, cam_pos_nodes)) {
370                         blocks_occlusion_culled++;
371                         continue;
372                 }
373
374                 // Block meshes are stored in blocks where all coordinates are even (lowest bit set to 0)
375                 // Add them to the de-dup set.
376                 shortlist.emplace(block_coord.X & ~1, block_coord.Y & ~1, block_coord.Z & ~1);
377                 // All other blocks we can grab and add to the drawlist right away.
378                 if (block && m_drawlist.emplace(block_coord, block).second) {
379                         // only grab the ref if the block exists and was not in the list
380                         block->refGrab();
381                 }
382
383                 // Decide which sides to traverse next or to block away
384
385                 // First, find the near sides that would occlude the far sides
386                 // * A near side can itself be occluded by a nearby block (the test above ^^)
387                 // * A near side can be visible but fully opaque by itself (e.g. ground at the 0 level)
388
389                 // mesh solid sides are +Z-Z+Y-Y+X-X
390                 // if we are inside the block's coordinates on an axis, 
391                 // treat these sides as opaque, as they should not allow to reach the far sides
392                 u8 block_inner_sides = (look.X == 0 ? 3 : 0) |
393                         (look.Y == 0 ? 12 : 0) |
394                         (look.Z == 0 ? 48 : 0);
395
396                 // get the mask for the sides that are relevant based on the direction
397                 u8 near_inner_sides = (look.X > 0 ? 1 : 2) |
398                                 (look.Y > 0 ? 4 : 8) |
399                                 (look.Z > 0 ? 16 : 32);
400                 
401                 // This bitset is +Z-Z+Y-Y+X-X (See MapBlockMesh), and axis is XYZ.
402                 // Get he block's transparent sides
403                 u8 transparent_sides = (occlusion_culling_enabled && block) ? ~block->solid_sides : 0x3F;
404
405                 // compress block transparent sides to ZYX mask of see-through axes
406                 u8 near_transparency =  (block_inner_sides == 0x3F) ? near_inner_sides : (transparent_sides & near_inner_sides);
407
408                 // when we are inside the camera block, do not block any sides
409                 if (block_inner_sides == 0x3F)
410                         block_inner_sides = 0;
411
412                 near_transparency &= ~block_inner_sides & 0x3F;
413
414                 near_transparency |= (near_transparency >> 1);
415                 near_transparency = (near_transparency & 1) |
416                                 ((near_transparency >> 1) & 2) |
417                                 ((near_transparency >> 2) & 4);
418
419                 // combine with known visible sides that matter
420                 near_transparency &= visible_outer_sides;
421
422                 // The rule for any far side to be visible:
423                 // * Any of the adjacent near sides is transparent (different axes)
424                 // * The opposite near side (same axis) is transparent, if it is the dominant axis of the look vector
425
426                 // Calculate vector from camera to mapblock center. Because we only need relation between
427                 // coordinates we scale by 2 to avoid precision loss.
428                 v3s16 precise_look = 2 * (block_pos_nodes - cam_pos_nodes) + MAP_BLOCKSIZE - 1;
429
430                 // dominant axis flag
431                 u8 dominant_axis = (abs(precise_look.X) > abs(precise_look.Y) && abs(precise_look.X) > abs(precise_look.Z)) |
432                                         ((abs(precise_look.Y) > abs(precise_look.Z) && abs(precise_look.Y) > abs(precise_look.X)) << 1) |
433                                         ((abs(precise_look.Z) > abs(precise_look.X) && abs(precise_look.Z) > abs(precise_look.Y)) << 2);
434
435                 // Queue next blocks for processing:
436                 // - Examine "far" sides of the current blocks, i.e. never move towards the camera
437                 // - Only traverse the sides that are not occluded
438                 // - Only traverse the sides that are not opaque
439                 // When queueing, mark the relevant side on the next block as 'visible'
440                 for (s16 axis = 0; axis < 3; axis++) {
441
442                         // Select a bit from transparent_sides for the side
443                         u8 far_side_mask = 1 << (2 * axis);
444
445                         // axis flag
446                         u8 my_side = 1 << axis;
447                         u8 adjacent_sides = my_side ^ 0x07;
448
449                         auto traverse_far_side = [&](s8 next_pos_offset) {
450                                 // far side is visible if adjacent near sides are transparent, or if opposite side on dominant axis is transparent
451                                 bool side_visible = ((near_transparency & adjacent_sides) | (near_transparency & my_side & dominant_axis)) != 0;
452                                 side_visible = side_visible && ((far_side_mask & transparent_sides) != 0);
453
454                                 v3s16 next_pos = block_coord;
455                                 next_pos[axis] += next_pos_offset;
456
457                                 // If a side is a see-through, mark the next block's side as visible, and queue
458                                 if (side_visible) {
459                                         auto &next_flags = blocks_seen.getChunk(next_pos).getBits(next_pos);
460                                         next_flags |= my_side;
461                                         blocks_to_consider.push(next_pos);
462                                 }
463                                 else {
464                                         sides_skipped++;
465                                 }
466                         };
467
468
469                         // Test the '-' direction of the axis
470                         if (look[axis] <= 0 && block_coord[axis] > p_blocks_min[axis])
471                                 traverse_far_side(-1);
472
473                         // Test the '+' direction of the axis
474                         far_side_mask <<= 1;
475
476                         if (look[axis] >= 0 && block_coord[axis] < p_blocks_max[axis])
477                                 traverse_far_side(+1);
478                 }
479         }
480
481         g_profiler->avg("MapBlocks shortlist [#]", shortlist.size());
482
483         for (auto pos : shortlist) {
484                 MapBlock * block = getBlockNoCreateNoEx(pos);
485                 if (block && m_drawlist.emplace(pos, block).second) {
486                         // only grab the ref if the block exists and was not in the list
487                         block->refGrab();
488                 }
489         }
490
491         g_profiler->avg("MapBlocks occlusion culled [#]", blocks_occlusion_culled);
492         g_profiler->avg("MapBlocks sides skipped [#]", sides_skipped);
493         g_profiler->avg("MapBlocks examined [#]", blocks_visited);
494         g_profiler->avg("MapBlocks drawn [#]", m_drawlist.size());
495 }
496
497 void ClientMap::touchMapBlocks()
498 {
499         v3s16 cam_pos_nodes = floatToInt(m_camera_position, BS);
500
501         v3s16 p_blocks_min;
502         v3s16 p_blocks_max;
503         getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
504
505         // Number of blocks currently loaded by the client
506         u32 blocks_loaded = 0;
507         // Number of blocks with mesh in rendering range
508         u32 blocks_in_range_with_mesh = 0;
509
510         for (const auto &sector_it : m_sectors) {
511                 MapSector *sector = sector_it.second;
512                 v2s16 sp = sector->getPos();
513
514                 blocks_loaded += sector->size();
515                 if (!m_control.range_all) {
516                         if (sp.X < p_blocks_min.X || sp.X > p_blocks_max.X ||
517                                         sp.Y < p_blocks_min.Z || sp.Y > p_blocks_max.Z)
518                                 continue;
519                 }
520
521                 MapBlockVect sectorblocks;
522                 sector->getBlocks(sectorblocks);
523
524                 /*
525                         Loop through blocks in sector
526                 */
527
528                 for (MapBlock *block : sectorblocks) {
529                         /*
530                                 Compare block position to camera position, skip
531                                 if not seen on display
532                         */
533
534                         if (!block->mesh) {
535                                 // Ignore if mesh doesn't exist
536                                 continue;
537                         }
538
539                         v3f mesh_sphere_center = intToFloat(block->getPosRelative(), BS)
540                                         + block->mesh->getBoundingSphereCenter();
541                         f32 mesh_sphere_radius = block->mesh->getBoundingRadius();
542                         // First, perform a simple distance check.
543                         if (!m_control.range_all &&
544                                 mesh_sphere_center.getDistanceFrom(intToFloat(cam_pos_nodes, BS)) >
545                                         m_control.wanted_range * BS + mesh_sphere_radius)
546                                 continue; // Out of range, skip.
547
548                         // Keep the block alive as long as it is in range.
549                         block->resetUsageTimer();
550                         blocks_in_range_with_mesh++;
551                 }
552         }
553         g_profiler->avg("MapBlock meshes in range [#]", blocks_in_range_with_mesh);
554         g_profiler->avg("MapBlocks loaded [#]", blocks_loaded);
555 }
556
557 void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
558 {
559         bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT;
560
561         std::string prefix;
562         if (pass == scene::ESNRP_SOLID)
563                 prefix = "renderMap(SOLID): ";
564         else
565                 prefix = "renderMap(TRANSPARENT): ";
566
567         /*
568                 This is called two times per frame, reset on the non-transparent one
569         */
570         if (pass == scene::ESNRP_SOLID)
571                 m_last_drawn_sectors.clear();
572
573         /*
574                 Get animation parameters
575         */
576         const float animation_time = m_client->getAnimationTime();
577         const int crack = m_client->getCrackLevel();
578         const u32 daynight_ratio = m_client->getEnv().getDayNightRatio();
579
580         const v3f camera_position = m_camera_position;
581
582         /*
583                 Get all blocks and draw all visible ones
584         */
585
586         u32 vertex_count = 0;
587         u32 drawcall_count = 0;
588
589         // For limiting number of mesh animations per frame
590         u32 mesh_animate_count = 0;
591         //u32 mesh_animate_count_far = 0;
592
593         /*
594                 Update transparent meshes
595         */
596         if (is_transparent_pass)
597                 updateTransparentMeshBuffers();
598
599         /*
600                 Draw the selected MapBlocks
601         */
602
603         MeshBufListList grouped_buffers;
604         std::vector<DrawDescriptor> draw_order;
605         video::SMaterial previous_material;
606
607         auto is_frustum_culled = m_client->getCamera()->getFrustumCuller();
608
609         for (auto &i : m_drawlist) {
610                 v3s16 block_pos = i.first;
611                 MapBlock *block = i.second;
612                 MapBlockMesh *block_mesh = block->mesh;
613
614                 // Meshes are only stored every 8-th block (where all coordinates are even),
615                 // but we keep all the visible blocks in the draw list to prevent client
616                 // from dropping them.
617                 // On top of that, in some cases block mesh can be removed
618                 // before the block is removed from the draw list.
619                 if (!block_mesh)
620                         continue;
621
622                 // Do exact frustum culling
623                 // (The one in updateDrawList is only coarse.)
624                 v3f mesh_sphere_center = intToFloat(block->getPosRelative(), BS)
625                                 + block_mesh->getBoundingSphereCenter();
626                 f32 mesh_sphere_radius = block_mesh->getBoundingRadius();
627                 if (is_frustum_culled(mesh_sphere_center, mesh_sphere_radius))
628                         continue;
629
630                 v3f block_pos_r = intToFloat(block->getPosRelative() + MAP_BLOCKSIZE / 2, BS);
631
632                 float d = camera_position.getDistanceFrom(block_pos_r);
633                 d = MYMAX(0,d - BLOCK_MAX_RADIUS);
634
635                 // Mesh animation
636                 if (pass == scene::ESNRP_SOLID) {
637                         // Pretty random but this should work somewhat nicely
638                         bool faraway = d >= BS * 50;
639                         if (block_mesh->isAnimationForced() || !faraway ||
640                                         mesh_animate_count < (m_control.range_all ? 200 : 50)) {
641
642                                 bool animated = block_mesh->animate(faraway, animation_time,
643                                         crack, daynight_ratio);
644                                 if (animated)
645                                         mesh_animate_count++;
646                         } else {
647                                 block_mesh->decreaseAnimationForceTimer();
648                         }
649                 }
650
651                 /*
652                         Get the meshbuffers of the block
653                 */
654                 if (is_transparent_pass) {
655                         // In transparent pass, the mesh will give us
656                         // the partial buffers in the correct order
657                         for (auto &buffer : block_mesh->getTransparentBuffers())
658                                 draw_order.emplace_back(block_pos, &buffer);
659                 }
660                 else {
661                         // otherwise, group buffers across meshes
662                         // using MeshBufListList
663                         for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
664                                 scene::IMesh *mesh = block_mesh->getMesh(layer);
665                                 assert(mesh);
666
667                                 u32 c = mesh->getMeshBufferCount();
668                                 for (u32 i = 0; i < c; i++) {
669                                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
670
671                                         video::SMaterial& material = buf->getMaterial();
672                                         video::IMaterialRenderer* rnd =
673                                                         driver->getMaterialRenderer(material.MaterialType);
674                                         bool transparent = (rnd && rnd->isTransparent());
675                                         if (!transparent) {
676                                                 if (buf->getVertexCount() == 0)
677                                                         errorstream << "Block [" << analyze_block(block)
678                                                                         << "] contains an empty meshbuf" << std::endl;
679
680                                                 grouped_buffers.add(buf, block_pos, layer);
681                                         }
682                                 }
683                         }
684                 }
685         }
686
687         // Capture draw order for all solid meshes
688         for (auto &lists : grouped_buffers.lists) {
689                 for (MeshBufList &list : lists) {
690                         // iterate in reverse to draw closest blocks first
691                         for (auto it = list.bufs.rbegin(); it != list.bufs.rend(); ++it) {
692                                 draw_order.emplace_back(it->first, it->second, it != list.bufs.rbegin());
693                         }
694                 }
695         }
696
697         TimeTaker draw("Drawing mesh buffers");
698
699         core::matrix4 m; // Model matrix
700         v3f offset = intToFloat(m_camera_offset, BS);
701         u32 material_swaps = 0;
702
703         // Render all mesh buffers in order
704         drawcall_count += draw_order.size();
705
706         for (auto &descriptor : draw_order) {
707                 scene::IMeshBuffer *buf = descriptor.getBuffer();
708
709                 if (!descriptor.m_reuse_material) {
710                         auto &material = buf->getMaterial();
711
712                         // Apply filter settings
713                         material.setFlag(video::EMF_TRILINEAR_FILTER,
714                                 m_cache_trilinear_filter);
715                         material.setFlag(video::EMF_BILINEAR_FILTER,
716                                 m_cache_bilinear_filter);
717                         material.setFlag(video::EMF_ANISOTROPIC_FILTER,
718                                 m_cache_anistropic_filter);
719                         material.setFlag(video::EMF_WIREFRAME,
720                                 m_control.show_wireframe);
721
722                         // pass the shadow map texture to the buffer texture
723                         ShadowRenderer *shadow = m_rendering_engine->get_shadow_renderer();
724                         if (shadow && shadow->is_active()) {
725                                 auto &layer = material.TextureLayer[ShadowRenderer::TEXTURE_LAYER_SHADOW];
726                                 layer.Texture = shadow->get_texture();
727                                 layer.TextureWrapU = video::E_TEXTURE_CLAMP::ETC_CLAMP_TO_EDGE;
728                                 layer.TextureWrapV = video::E_TEXTURE_CLAMP::ETC_CLAMP_TO_EDGE;
729                                 // Do not enable filter on shadow texture to avoid visual artifacts
730                                 // with colored shadows.
731                                 // Filtering is done in shader code anyway
732                                 layer.BilinearFilter = false;
733                                 layer.AnisotropicFilter = false;
734                                 layer.TrilinearFilter = false;
735                         }
736                         driver->setMaterial(material);
737                         ++material_swaps;
738                         material.TextureLayer[ShadowRenderer::TEXTURE_LAYER_SHADOW].Texture = nullptr;
739                 }
740
741                 v3f block_wpos = intToFloat(descriptor.m_pos / 8 * 8 * MAP_BLOCKSIZE, BS);
742                 m.setTranslation(block_wpos - offset);
743
744                 driver->setTransform(video::ETS_WORLD, m);
745                 descriptor.draw(driver);
746                 vertex_count += buf->getIndexCount();
747         }
748
749         g_profiler->avg(prefix + "draw meshes [ms]", draw.stop(true));
750
751         // Log only on solid pass because values are the same
752         if (pass == scene::ESNRP_SOLID) {
753                 g_profiler->avg("renderMap(): animated meshes [#]", mesh_animate_count);
754         }
755
756         if (pass == scene::ESNRP_TRANSPARENT) {
757                 g_profiler->avg("renderMap(): transparent buffers [#]", draw_order.size());
758         }
759
760         g_profiler->avg(prefix + "vertices drawn [#]", vertex_count);
761         g_profiler->avg(prefix + "drawcalls [#]", drawcall_count);
762         g_profiler->avg(prefix + "material swaps [#]", material_swaps);
763 }
764
765 static bool getVisibleBrightness(Map *map, const v3f &p0, v3f dir, float step,
766         float step_multiplier, float start_distance, float end_distance,
767         const NodeDefManager *ndef, u32 daylight_factor, float sunlight_min_d,
768         int *result, bool *sunlight_seen)
769 {
770         int brightness_sum = 0;
771         int brightness_count = 0;
772         float distance = start_distance;
773         dir.normalize();
774         v3f pf = p0;
775         pf += dir * distance;
776         int noncount = 0;
777         bool nonlight_seen = false;
778         bool allow_allowing_non_sunlight_propagates = false;
779         bool allow_non_sunlight_propagates = false;
780         // Check content nearly at camera position
781         {
782                 v3s16 p = floatToInt(p0 /*+ dir * 3*BS*/, BS);
783                 MapNode n = map->getNode(p);
784                 if(ndef->getLightingFlags(n).has_light &&
785                                 !ndef->getLightingFlags(n).sunlight_propagates)
786                         allow_allowing_non_sunlight_propagates = true;
787         }
788         // If would start at CONTENT_IGNORE, start closer
789         {
790                 v3s16 p = floatToInt(pf, BS);
791                 MapNode n = map->getNode(p);
792                 if(n.getContent() == CONTENT_IGNORE){
793                         float newd = 2*BS;
794                         pf = p0 + dir * 2*newd;
795                         distance = newd;
796                         sunlight_min_d = 0;
797                 }
798         }
799         for (int i=0; distance < end_distance; i++) {
800                 pf += dir * step;
801                 distance += step;
802                 step *= step_multiplier;
803
804                 v3s16 p = floatToInt(pf, BS);
805                 MapNode n = map->getNode(p);
806                 ContentLightingFlags f = ndef->getLightingFlags(n);
807                 if (allow_allowing_non_sunlight_propagates && i == 0 &&
808                                 f.has_light && !f.sunlight_propagates) {
809                         allow_non_sunlight_propagates = true;
810                 }
811
812                 if (!f.has_light || (!f.sunlight_propagates && !allow_non_sunlight_propagates)){
813                         nonlight_seen = true;
814                         noncount++;
815                         if(noncount >= 4)
816                                 break;
817                         continue;
818                 }
819
820                 if (distance >= sunlight_min_d && !*sunlight_seen && !nonlight_seen)
821                         if (n.getLight(LIGHTBANK_DAY, f) == LIGHT_SUN)
822                                 *sunlight_seen = true;
823                 noncount = 0;
824                 brightness_sum += decode_light(n.getLightBlend(daylight_factor, f));
825                 brightness_count++;
826         }
827         *result = 0;
828         if(brightness_count == 0)
829                 return false;
830         *result = brightness_sum / brightness_count;
831         /*std::cerr<<"Sampled "<<brightness_count<<" points; result="
832                         <<(*result)<<std::endl;*/
833         return true;
834 }
835
836 int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
837                 int oldvalue, bool *sunlight_seen_result)
838 {
839         ScopeProfiler sp(g_profiler, "CM::getBackgroundBrightness", SPT_AVG);
840         static v3f z_directions[50] = {
841                 v3f(-100, 0, 0)
842         };
843         static f32 z_offsets[50] = {
844                 -1000,
845         };
846
847         if (z_directions[0].X < -99) {
848                 for (u32 i = 0; i < ARRLEN(z_directions); i++) {
849                         // Assumes FOV of 72 and 16/9 aspect ratio
850                         z_directions[i] = v3f(
851                                 0.02 * myrand_range(-100, 100),
852                                 1.0,
853                                 0.01 * myrand_range(-100, 100)
854                         ).normalize();
855                         z_offsets[i] = 0.01 * myrand_range(0,100);
856                 }
857         }
858
859         int sunlight_seen_count = 0;
860         float sunlight_min_d = max_d*0.8;
861         if(sunlight_min_d > 35*BS)
862                 sunlight_min_d = 35*BS;
863         std::vector<int> values;
864         values.reserve(ARRLEN(z_directions));
865         for (u32 i = 0; i < ARRLEN(z_directions); i++) {
866                 v3f z_dir = z_directions[i];
867                 core::CMatrix4<f32> a;
868                 a.buildRotateFromTo(v3f(0,1,0), z_dir);
869                 v3f dir = m_camera_direction;
870                 a.rotateVect(dir);
871                 int br = 0;
872                 float step = BS*1.5;
873                 if(max_d > 35*BS)
874                         step = max_d / 35 * 1.5;
875                 float off = step * z_offsets[i];
876                 bool sunlight_seen_now = false;
877                 bool ok = getVisibleBrightness(this, m_camera_position, dir,
878                                 step, 1.0, max_d*0.6+off, max_d, m_nodedef, daylight_factor,
879                                 sunlight_min_d,
880                                 &br, &sunlight_seen_now);
881                 if(sunlight_seen_now)
882                         sunlight_seen_count++;
883                 if(!ok)
884                         continue;
885                 values.push_back(br);
886                 // Don't try too much if being in the sun is clear
887                 if(sunlight_seen_count >= 20)
888                         break;
889         }
890         int brightness_sum = 0;
891         int brightness_count = 0;
892         std::sort(values.begin(), values.end());
893         u32 num_values_to_use = values.size();
894         if(num_values_to_use >= 10)
895                 num_values_to_use -= num_values_to_use/2;
896         else if(num_values_to_use >= 7)
897                 num_values_to_use -= num_values_to_use/3;
898         u32 first_value_i = (values.size() - num_values_to_use) / 2;
899
900         for (u32 i=first_value_i; i < first_value_i + num_values_to_use; i++) {
901                 brightness_sum += values[i];
902                 brightness_count++;
903         }
904
905         int ret = 0;
906         if(brightness_count == 0){
907                 MapNode n = getNode(floatToInt(m_camera_position, BS));
908                 ContentLightingFlags f = m_nodedef->getLightingFlags(n);
909                 if(f.has_light){
910                         ret = decode_light(n.getLightBlend(daylight_factor, f));
911                 } else {
912                         ret = oldvalue;
913                 }
914         } else {
915                 ret = brightness_sum / brightness_count;
916         }
917
918         *sunlight_seen_result = (sunlight_seen_count > 0);
919         return ret;
920 }
921
922 void ClientMap::renderPostFx(CameraMode cam_mode)
923 {
924         // Sadly ISceneManager has no "post effects" render pass, in that case we
925         // could just register for that and handle it in renderMap().
926
927         MapNode n = getNode(floatToInt(m_camera_position, BS));
928
929         const ContentFeatures& features = m_nodedef->get(n);
930         video::SColor post_effect_color = features.post_effect_color;
931
932         // If the camera is in a solid node, make everything black.
933         // (first person mode only)
934         if (features.solidness == 2 && cam_mode == CAMERA_MODE_FIRST &&
935                 !m_control.allow_noclip) {
936                 post_effect_color = video::SColor(255, 0, 0, 0);
937         }
938
939         if (post_effect_color.getAlpha() != 0) {
940                 // Draw a full-screen rectangle
941                 video::IVideoDriver* driver = SceneManager->getVideoDriver();
942                 v2u32 ss = driver->getScreenSize();
943                 core::rect<s32> rect(0,0, ss.X, ss.Y);
944                 driver->draw2DRectangle(post_effect_color, rect);
945         }
946 }
947
948 void ClientMap::PrintInfo(std::ostream &out)
949 {
950         out<<"ClientMap: ";
951 }
952
953 void ClientMap::renderMapShadows(video::IVideoDriver *driver,
954                 const video::SMaterial &material, s32 pass, int frame, int total_frames)
955 {
956         bool is_transparent_pass = pass != scene::ESNRP_SOLID;
957         std::string prefix;
958         if (is_transparent_pass)
959                 prefix = "renderMap(SHADOW TRANS): ";
960         else
961                 prefix = "renderMap(SHADOW SOLID): ";
962
963         u32 drawcall_count = 0;
964         u32 vertex_count = 0;
965
966         MeshBufListList grouped_buffers;
967         std::vector<DrawDescriptor> draw_order;
968
969
970         int count = 0;
971         int low_bound = is_transparent_pass ? 0 : m_drawlist_shadow.size() / total_frames * frame;
972         int high_bound = is_transparent_pass ? m_drawlist_shadow.size() : m_drawlist_shadow.size() / total_frames * (frame + 1);
973
974         // transparent pass should be rendered in one go
975         if (is_transparent_pass && frame != total_frames - 1) {
976                 return;
977         }
978
979         for (const auto &i : m_drawlist_shadow) {
980                 // only process specific part of the list & break early
981                 ++count;
982                 if (count <= low_bound)
983                         continue;
984                 if (count > high_bound)
985                         break;
986
987                 v3s16 block_pos = i.first;
988                 MapBlock *block = i.second;
989
990                 // If the mesh of the block happened to get deleted, ignore it
991                 if (!block->mesh)
992                         continue;
993
994                 /*
995                         Get the meshbuffers of the block
996                 */
997                 if (is_transparent_pass) {
998                         // In transparent pass, the mesh will give us
999                         // the partial buffers in the correct order
1000                         for (auto &buffer : block->mesh->getTransparentBuffers())
1001                                 draw_order.emplace_back(block_pos, &buffer);
1002                 }
1003                 else {
1004                         // otherwise, group buffers across meshes
1005                         // using MeshBufListList
1006                         MapBlockMesh *mapBlockMesh = block->mesh;
1007                         assert(mapBlockMesh);
1008
1009                         for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
1010                                 scene::IMesh *mesh = mapBlockMesh->getMesh(layer);
1011                                 assert(mesh);
1012
1013                                 u32 c = mesh->getMeshBufferCount();
1014                                 for (u32 i = 0; i < c; i++) {
1015                                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
1016
1017                                         video::SMaterial &mat = buf->getMaterial();
1018                                         auto rnd = driver->getMaterialRenderer(mat.MaterialType);
1019                                         bool transparent = rnd && rnd->isTransparent();
1020                                         if (!transparent)
1021                                                 grouped_buffers.add(buf, block_pos, layer);
1022                                 }
1023                         }
1024                 }
1025         }
1026
1027         u32 buffer_count = 0;
1028         for (auto &lists : grouped_buffers.lists)
1029                 for (MeshBufList &list : lists)
1030                         buffer_count += list.bufs.size();
1031
1032         draw_order.reserve(draw_order.size() + buffer_count);
1033
1034         // Capture draw order for all solid meshes
1035         for (auto &lists : grouped_buffers.lists) {
1036                 for (MeshBufList &list : lists) {
1037                         // iterate in reverse to draw closest blocks first
1038                         for (auto it = list.bufs.rbegin(); it != list.bufs.rend(); ++it)
1039                                 draw_order.emplace_back(it->first, it->second, it != list.bufs.rbegin());
1040                 }
1041         }
1042
1043         TimeTaker draw("Drawing shadow mesh buffers");
1044
1045         core::matrix4 m; // Model matrix
1046         v3f offset = intToFloat(m_camera_offset, BS);
1047         u32 material_swaps = 0;
1048
1049         // Render all mesh buffers in order
1050         drawcall_count += draw_order.size();
1051
1052         for (auto &descriptor : draw_order) {
1053                 scene::IMeshBuffer *buf = descriptor.getBuffer();
1054
1055                 if (!descriptor.m_reuse_material) {
1056                         // override some material properties
1057                         video::SMaterial local_material = buf->getMaterial();
1058                         local_material.MaterialType = material.MaterialType;
1059                         local_material.BackfaceCulling = material.BackfaceCulling;
1060                         local_material.FrontfaceCulling = material.FrontfaceCulling;
1061                         local_material.BlendOperation = material.BlendOperation;
1062                         local_material.Lighting = false;
1063                         driver->setMaterial(local_material);
1064                         ++material_swaps;
1065                 }
1066
1067                 v3f block_wpos = intToFloat(descriptor.m_pos / 8 * 8 * MAP_BLOCKSIZE, BS);
1068                 m.setTranslation(block_wpos - offset);
1069
1070                 driver->setTransform(video::ETS_WORLD, m);
1071                 descriptor.draw(driver);
1072                 vertex_count += buf->getIndexCount();
1073         }
1074
1075         // restore the driver material state
1076         video::SMaterial clean;
1077         clean.BlendOperation = video::EBO_ADD;
1078         driver->setMaterial(clean); // reset material to defaults
1079         driver->draw3DLine(v3f(), v3f(), video::SColor(0));
1080
1081         g_profiler->avg(prefix + "draw meshes [ms]", draw.stop(true));
1082         g_profiler->avg(prefix + "vertices drawn [#]", vertex_count);
1083         g_profiler->avg(prefix + "drawcalls [#]", drawcall_count);
1084         g_profiler->avg(prefix + "material swaps [#]", material_swaps);
1085 }
1086
1087 /*
1088         Custom update draw list for the pov of shadow light.
1089 */
1090 void ClientMap::updateDrawListShadow(v3f shadow_light_pos, v3f shadow_light_dir, float radius, float length)
1091 {
1092         ScopeProfiler sp(g_profiler, "CM::updateDrawListShadow()", SPT_AVG);
1093
1094         v3s16 cam_pos_nodes = floatToInt(shadow_light_pos, BS);
1095         v3s16 p_blocks_min;
1096         v3s16 p_blocks_max;
1097         getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max, radius + length);
1098
1099         for (auto &i : m_drawlist_shadow) {
1100                 MapBlock *block = i.second;
1101                 block->refDrop();
1102         }
1103         m_drawlist_shadow.clear();
1104
1105         // Number of blocks currently loaded by the client
1106         u32 blocks_loaded = 0;
1107         // Number of blocks with mesh in rendering range
1108         u32 blocks_in_range_with_mesh = 0;
1109         // Number of blocks occlusion culled
1110         u32 blocks_occlusion_culled = 0;
1111
1112         for (auto &sector_it : m_sectors) {
1113                 MapSector *sector = sector_it.second;
1114                 if (!sector)
1115                         continue;
1116                 blocks_loaded += sector->size();
1117
1118                 MapBlockVect sectorblocks;
1119                 sector->getBlocks(sectorblocks);
1120
1121                 /*
1122                         Loop through blocks in sector
1123                 */
1124                 for (MapBlock *block : sectorblocks) {
1125                         if (!block->mesh) {
1126                                 // Ignore if mesh doesn't exist
1127                                 continue;
1128                         }
1129
1130                         v3f block_pos = intToFloat(block->getPos() * MAP_BLOCKSIZE, BS);
1131                         v3f projection = shadow_light_pos + shadow_light_dir * shadow_light_dir.dotProduct(block_pos - shadow_light_pos);
1132                         if (projection.getDistanceFrom(block_pos) > radius)
1133                                 continue;
1134
1135                         blocks_in_range_with_mesh++;
1136
1137                         // This block is in range. Reset usage timer.
1138                         block->resetUsageTimer();
1139
1140                         // Add to set
1141                         if (m_drawlist_shadow.find(block->getPos()) == m_drawlist_shadow.end()) {
1142                                 block->refGrab();
1143                                 m_drawlist_shadow[block->getPos()] = block;
1144                         }
1145                 }
1146         }
1147
1148         g_profiler->avg("SHADOW MapBlock meshes in range [#]", blocks_in_range_with_mesh);
1149         g_profiler->avg("SHADOW MapBlocks occlusion culled [#]", blocks_occlusion_culled);
1150         g_profiler->avg("SHADOW MapBlocks drawn [#]", m_drawlist_shadow.size());
1151         g_profiler->avg("SHADOW MapBlocks loaded [#]", blocks_loaded);
1152 }
1153
1154 void ClientMap::reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks)
1155 {
1156         g_profiler->avg("CM::reportMetrics loaded blocks [#]", all_blocks);
1157 }
1158
1159 void ClientMap::updateTransparentMeshBuffers()
1160 {
1161         ScopeProfiler sp(g_profiler, "CM::updateTransparentMeshBuffers", SPT_AVG);
1162         u32 sorted_blocks = 0;
1163         u32 unsorted_blocks = 0;
1164         f32 sorting_distance_sq = pow(m_cache_transparency_sorting_distance * BS, 2.0f);
1165
1166
1167         // Update the order of transparent mesh buffers in each mesh
1168         for (auto it = m_drawlist.begin(); it != m_drawlist.end(); it++) {
1169                 MapBlock* block = it->second;
1170                 if (!block->mesh)
1171                         continue;
1172
1173                 if (m_needs_update_transparent_meshes ||
1174                                 block->mesh->getTransparentBuffers().size() == 0) {
1175
1176                         v3s16 block_pos = block->getPos();
1177                         v3f block_pos_f = intToFloat(block_pos * MAP_BLOCKSIZE + MAP_BLOCKSIZE / 2, BS);
1178                         f32 distance = m_camera_position.getDistanceFromSQ(block_pos_f);
1179                         if (distance <= sorting_distance_sq) {
1180                                 block->mesh->updateTransparentBuffers(m_camera_position, block_pos);
1181                                 ++sorted_blocks;
1182                         }
1183                         else {
1184                                 block->mesh->consolidateTransparentBuffers();
1185                                 ++unsorted_blocks;
1186                         }
1187                 }
1188         }
1189
1190         g_profiler->avg("CM::Transparent Buffers - Sorted", sorted_blocks);
1191         g_profiler->avg("CM::Transparent Buffers - Unsorted", unsorted_blocks);
1192         m_needs_update_transparent_meshes = false;
1193 }
1194
1195 scene::IMeshBuffer* ClientMap::DrawDescriptor::getBuffer()
1196 {
1197         return m_use_partial_buffer ? m_partial_buffer->getBuffer() : m_buffer;
1198 }
1199
1200 void ClientMap::DrawDescriptor::draw(video::IVideoDriver* driver)
1201 {
1202         if (m_use_partial_buffer) {
1203                 m_partial_buffer->beforeDraw();
1204                 driver->drawMeshBuffer(m_partial_buffer->getBuffer());
1205                 m_partial_buffer->afterDraw();
1206         } else {
1207                 driver->drawMeshBuffer(m_buffer);
1208         }
1209 }