--- a/rts/Game/WebbarPoses.h +++ b/rts/Game/WebbarPoses.h @@ -22,6 +22,7 @@ // position). // Unsynced and read-only: nothing here changes simulation state or the engine's own model caches. #include +#include #include #include #include @@ -39,7 +40,7 @@ int32_t cameraSeq; // written by the page: > 0 once view[] holds a camera uint32_t slotCapacity; // bytes per slot uint32_t slotOffset[3]; // from the start of the block - uint32_t reserved; + uint32_t requestedFeatures; // page opt-in; zero preserves the original WPM1 slot layout // Written by the page: 6 normalized frustum planes (a, b, c, d; inside when a*x + b*y + c*z + d >= -r), // eye xyz, draw distance (0 = unlimited), LOD distances (near: every publication, beyond far: every // fourth, between: every second; 0 = no LOD), culling margin in elmos. @@ -47,10 +48,17 @@ // Written by the engine: publications, slot bytes, legacy WBM2 bytes for the same units, units with // data, piece records, culled units, LOD-skipped units, slots that had to grow. double stats[8]; + uint32_t extensionMagic; // 'WBX1'; old readers ignore this tail + uint32_t supportedFeatures; + // Optional reflected camera: normalized planes, eye xyz and one reserved float. + // Published by the page before cameraSeq, like the existing main camera. + float reflectionView[28]; }; -static_assert(sizeof(Header) == 232); -constexpr uint32_t HEADER_BYTES = 256, SLOT_HEADER_BYTES = 32; -constexpr uint8_t FLAG_ROTATION = 1, FLAG_CULLED = 2, FLAG_LOD = 4, FLAG_RESET = 8, FLAG_STRUCTURE = 16; +static_assert(offsetof(Header, extensionMagic) == 232); +static_assert(sizeof(Header) == 352); +constexpr uint32_t HEADER_BYTES = 384, SLOT_HEADER_BYTES = 32; +constexpr uint32_t EXTENSION_MAGIC = 0x31584257, FEATURE_BOUNDS = 1, FEATURE_REFLECTION = 2; +constexpr uint8_t FLAG_ROTATION = 1, FLAG_CULLED = 2, FLAG_LOD = 4, FLAG_RESET = 8, FLAG_STRUCTURE = 16, FLAG_NATIVE_BOUNDS = 32; struct PieceState { float raw[7]; uint32_t visible; float m[12]; int32_t first; bool dirty; }; struct UnitState { @@ -94,12 +102,15 @@ std::memset(memory, 0, HEADER_BYTES + 3 * size_t(next)); Header* h = reinterpret_cast(memory); h->magic = 0x314d5057; h->version = 1; h->middle = 1; h->slotCapacity = next; + h->extensionMagic = EXTENSION_MAGIC; h->supportedFeatures = FEATURE_BOUNDS | FEATURE_REFLECTION; for (int i = 0; i < 3; ++i) h->slotOffset[i] = HEADER_BYTES + i * next; if (header != nullptr) { // Keep the page's acknowledgement, camera and statistics across the switch. h->consumed = __atomic_load_n(&header->consumed, __ATOMIC_ACQUIRE); h->cameraSeq = header->cameraSeq; + h->requestedFeatures = __atomic_load_n(&header->requestedFeatures, __ATOMIC_ACQUIRE); std::memcpy(h->view, header->view, sizeof(h->view)); + std::memcpy(h->reflectionView, header->reflectionView, sizeof(h->reflectionView)); std::memcpy(h->stats, header->stats, sizeof(h->stats)); h->stats[7] += 1; } @@ -120,6 +131,11 @@ float view[32]; std::memcpy(view, header->view, sizeof(view)); const int32_t cameraSeq = __atomic_load_n(&header->cameraSeq, __ATOMIC_ACQUIRE); + const uint32_t features = __atomic_load_n(&header->requestedFeatures, __ATOMIC_ACQUIRE) & header->supportedFeatures; + const bool publishBounds = (features & FEATURE_BOUNDS) != 0; + const bool reflectionKnown = cameraSeq > 0 && (features & FEATURE_REFLECTION) != 0; + float reflection[28]; + if (reflectionKnown) std::memcpy(reflection, header->reflectionView, sizeof(reflection)); const bool viewKnown = cameraSeq > 0; const float reach = view[27], lodNear = view[28], lodFar = view[29], margin = view[30]; Writer out{block + header->slotOffset[back], SLOT_HEADER_BYTES, capacity}; @@ -141,12 +157,27 @@ flags |= FLAG_RESET; } st.present = slotSeq; + float bounds[4] = {}; + if (publishBounds) { + // Read the same cached LocalModel box as the original UnitDrawerData. + // Never call UpdateBoundingVolume/UpdateChildTransform from this UI + // publisher: those updates belong to the original simulation cadence. + const float3 rel = unit->localModel.GetRelMidPos(); + const float3 offset = unit->GetObjectSpaceVec(float3(-rel.x, rel.y, rel.z)); + bounds[0] = offset.x; bounds[1] = offset.y; bounds[2] = offset.z; bounds[3] = unit->GetDrawRadius(); + flags |= FLAG_NATIVE_BOUNDS; + } bool active = true; if (viewKnown) { - const float3 c = unit->midPos; - const float r = unit->radius * 1.5f + std::max(0.0f, margin); + const float3 c = publishBounds ? unit->pos + float3(bounds[0], bounds[1], bounds[2]) : float3(unit->midPos); + const float r = (publishBounds ? bounds[3] : unit->radius * 1.5f) + std::max(0.0f, margin); for (int k = 0; k < 24 && active; k += 4) if (view[k] * c.x + view[k + 1] * c.y + view[k + 2] * c.z + view[k + 3] < -r) active = false; + if (!active && reflectionKnown) { + active = true; + for (int k = 0; k < 24 && active; k += 4) + if (reflection[k] * c.x + reflection[k + 1] * c.y + reflection[k + 2] * c.z + reflection[k + 3] < -r) active = false; + } const float dx = c.x - view[24], dy = c.y - view[25], dz = c.z - view[26], d2 = dx * dx + dy * dy + dz * dz; if (active && reach > 0.0f && d2 > (reach + r) * (reach + r)) active = false; if (!active) { flags |= FLAG_CULLED; culled += 1; } @@ -160,6 +191,7 @@ st.rotFirst = -1; st.structFirst = -1; for (PieceState& p: st.pieces) p.first = -1; out.Put(id); out.Put(uint16_t(def)); out.Put(uint8_t(unit->team)); out.Put(flags); out.Put(uint16_t(pieces.size())); out.Put(uint16_t(0)); + if (publishBounds) out.Bytes(bounds, sizeof(bounds)); ++count; continue; } @@ -205,6 +237,7 @@ if (sendRot) flags |= FLAG_ROTATION; if (sendStruct) flags |= FLAG_STRUCTURE; out.Put(id); out.Put(uint16_t(def)); out.Put(uint8_t(unit->team)); out.Put(flags); out.Put(uint16_t(pieces.size())); out.Put(included); + if (publishBounds) out.Bytes(bounds, sizeof(bounds)); if (sendStruct) for (const auto& piece: pieces) out.Put(uint16_t(piece.parent ? piece.parent->GetLModelPieceIndex() : 0xffff)); if (sendStruct && (pieces.size() & 1)) out.Put(uint16_t(0)); if (sendRot) out.Bytes(st.rot, sizeof(st.rot)); @@ -249,7 +282,7 @@ if (Write(units, slotSeq)) return; // Units are pending again (chains reset); a larger block gets a fresh triple buffer. const size_t pieces = std::accumulate(units.begin(), units.end(), size_t(0), [](size_t n, const CUnit* u) { return n + u->localModel.pieces.size(); }); - Allocate(uint32_t(SLOT_HEADER_BYTES + units.size() * 52 + pieces * 54 + 4096)); + Allocate(uint32_t(SLOT_HEADER_BYTES + units.size() * 68 + pieces * 54 + 4096)); } }