Phase 30 (2/n) : geometrie compacte, pool de sous-allocation, early-Z

- Formats GPU : le format de travail Vertex (68 o) n'est plus televerse.
  Objets et vegetation : ObjectVertex 44 o (conversion exacte). Terrain :
  TerrainVertex 16 o (deux altitudes f32 + deux normales snorm16) ; x/z,
  cible de morphing et UV sont recalcules par terrain.vert depuis
  gl_VertexIndex et DrawItem::grid, au bit pres (`precise`).
- Topologie des tuiles televersee UNE fois, en uint16 (96 Ko de moins par
  tuile). Indices des maillages d'objets en uint16.
- GpuBufferPool + RangeAllocator : toute la geometrie dans des blocs de
  4 Mo ; ecriture DIRECTE en memoire unifiee (GPU integre, lavapipe),
  staging conserve sur GPU discret (--force-staging pour l'eprouver),
  barriere ecriture-apres-lecture ajoutee devant les copies (validation
  de synchronisation a zero).
- Rendu : terrain dessine apres la vegetation (early-Z) et culling de ses
  faces arriere.
- Tests : +16 verifications (formats, positions recalculees, allocateur).

Survol 900 m/s (lavapipe) : pic GPU 286 -> 89 Mo, geometrie terrain
188 -> 33 Mo, ~17 000 -> 21 vkAllocateMemory, streaming 4,5 -> 1,6 ms
par image. Foret : 8,3 -> 12,4 images/s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MG5dK27RsN97DDm7CQRhx2
Cette révision appartient à :
Claude a révisé 2026-10-13 23:08:23 +00:00
1 Parent 8bb3222924
révision 3465b8e18b
26 fichiers modifiés
+1845 -463

Aucun fichier ne correspond à votre recherche

+12 -99
Voir le fichier
@@ -1,107 +1,20 @@
#include "renderer/Mesh.hpp"
#include "core/Logger.hpp"
#include "renderer/MeshData.hpp"
#include "renderer/VulkanBuffer.hpp"
#include "renderer/VulkanContext.hpp"
#include <stdexcept>
#include <string>
namespace vercora {
Mesh::Mesh(VulkanContext& context,
BufferUploader& uploader,
const MeshData& data,
bool log) {
// --- Validation AVANT toute allocation Vulkan ---------------------------
// Un index hors limites ne produit pas d'erreur a la creation du buffer :
// il se manifesterait au draw, sous forme de lecture hors du vertex buffer
// (comportement indefini cote GPU). Mieux vaut refuser ici, avec un message
// qui dit quoi.
if (data.vertices.empty()) {
throw std::runtime_error("Mesh vide : aucun sommet (" + data.name + ").");
Mesh::Mesh(GpuBufferPool& pool, VertexLayout layout,
const GpuRange& vertices, uint32_t vertexCount,
const GpuRange& indices, VkIndexType indexType, uint32_t indexCount,
bool ownsIndices)
: pool_(pool), layout_(layout), vertices_(vertices), indices_(indices),
indexType_(indexType), vertexCount_(vertexCount), indexCount_(indexCount),
ownsIndices_(ownsIndices) {}
Mesh::~Mesh() {
pool_.release(vertices_);
if (ownsIndices_) {
pool_.release(indices_);
}
if (data.indices.empty()) {
throw std::runtime_error("Mesh vide : aucun index (" + data.name + ").");
}
if (data.indices.size() % 3 != 0) {
throw std::runtime_error(
"Mesh non triangule : " + std::to_string(data.indices.size()) +
" index, ce qui n'est pas un multiple de 3 (" + data.name + ").");
}
const auto vertexCount = static_cast<uint32_t>(data.vertices.size());
for (uint32_t index : data.indices) {
if (index >= vertexCount) {
throw std::runtime_error(
"Index hors limites dans le mesh : " + std::to_string(index) +
" pour " + std::to_string(vertexCount) + " sommets (" + data.name + ").");
}
}
vertexCount_ = vertexCount;
indexCount_ = static_cast<uint32_t>(data.indices.size());
// --- Upload GPU ---------------------------------------------------------
vertexBuffer_ = createDeviceLocalBuffer(
context, uploader,
data.vertices.data(),
sizeof(data.vertices[0]) * data.vertices.size(),
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
indexBuffer_ = createDeviceLocalBuffer(
context, uploader,
data.indices.data(),
sizeof(data.indices[0]) * data.indices.size(),
VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
if (log) {
logInfo("Mesh charge sur le GPU : " + data.name + " - " +
std::to_string(vertexCount_) + " sommets, " +
std::to_string(indexCount_ / 3) + " triangles.");
}
}
Mesh::~Mesh() = default; // les buffers se detruisent avec leurs unique_ptr
std::unique_ptr<VulkanBuffer> Mesh::createDeviceLocalBuffer(
VulkanContext& context,
BufferUploader& uploader,
const void* data,
VkDeviceSize size,
VkBufferUsageFlags usage) {
// 1. Staging host-visible : accessible au CPU, on y ecrit la geometrie.
auto staging = std::make_unique<VulkanBuffer>(
context, size,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
staging->writeToBuffer(data, size);
// 2. Buffer final device-local : rapide pour le GPU, non mappable.
auto buffer = std::make_unique<VulkanBuffer>(
context, size,
VK_BUFFER_USAGE_TRANSFER_DST_BIT | usage,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
// 3. PHASE 12 : la copie est CONFIEE, pas attendue. Le staging part avec
// elle - il doit survivre jusqu'a l'execution de la frame qui portera la
// commande de copie, ce qui n'est plus une duree connue ici. C'est tout
// le changement : avant, cette ligne bloquait le thread jusqu'a ce que le
// GPU ait vide sa queue.
uploader.enqueueCopy(std::move(staging), buffer->handle());
return buffer;
}
void Mesh::bind(VkCommandBuffer commandBuffer) const {
const VkBuffer buffers[] = { vertexBuffer_->handle() };
const VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(commandBuffer, 0, 1, buffers, offsets);
// uint32 : suffisant pour de gros meshes, indispensable pour du terrain
// LiDAR plus tard (uint16 plafonnerait a 65536 sommets).
vkCmdBindIndexBuffer(commandBuffer, indexBuffer_->handle(), 0, VK_INDEX_TYPE_UINT32);
}
void Mesh::draw(VkCommandBuffer commandBuffer) const {