|
13 | 13 | namespace rive::ore |
14 | 14 | { |
15 | 15 |
|
16 | | -// --- Public method definitions (D3D12-only builds) --- |
17 | | -// When both D3D11 and D3D12 are compiled, the combined |
18 | | -// ore_context_d3d11_d3d12.cpp file provides these methods with dispatch. |
| 16 | +// Guarded by ORE_BACKEND_D3D12. The Windows build compiles both the d3d11 and |
| 17 | +// d3d12 dirs with both backend macros defined; BufferD3D11 and BufferD3D12 are |
| 18 | +// distinct classes under their own guards and the context factory selects the |
| 19 | +// concrete type at runtime, so there is no shared dispatch file. |
19 | 20 | #if defined(ORE_BACKEND_D3D12) |
20 | 21 |
|
| 22 | +void BufferD3D12::markBound() |
| 23 | +{ |
| 24 | + m_currentSerial = m_ctx->currentFrameNumber(); |
| 25 | + m_boundSinceUpdate = true; |
| 26 | +} |
| 27 | + |
| 28 | +bool BufferD3D12::acquireFreshBacking(uint32_t writeOffset, uint32_t writeSize) |
| 29 | +{ |
| 30 | + // Retire the current backing since an in flight bind may still read it. |
| 31 | + void* oldMapped = m_d3dMappedPtr; |
| 32 | + m_retired.push_back( |
| 33 | + {std::move(m_d3dBuffer), m_d3dMappedPtr, m_currentSerial}); |
| 34 | + |
| 35 | + // Reuse a retired backing the GPU has finished with, else allocate one. |
| 36 | + // Require the bind to predate the current frame, not just safeFrameNumber, |
| 37 | + // so a host that ever reports safe == current cannot hand back an in flight |
| 38 | + // backing. |
| 39 | + const uint64_t safe = m_ctx->safeFrameNumber(); |
| 40 | + const uint64_t current = m_ctx->currentFrameNumber(); |
| 41 | + for (auto it = m_retired.begin(); it != m_retired.end(); ++it) |
| 42 | + { |
| 43 | + if (it->serial <= safe && it->serial < current) |
| 44 | + { |
| 45 | + m_d3dBuffer = std::move(it->resource); |
| 46 | + m_d3dMappedPtr = it->mapped; |
| 47 | + m_retired.erase(it); |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | + if (m_d3dBuffer == nullptr && |
| 52 | + !m_ctx->d3d12AllocBufferBacking(m_allocatedSize, |
| 53 | + m_d3dBuffer, |
| 54 | + &m_d3dMappedPtr)) |
| 55 | + { |
| 56 | + // Allocation failed. Keep the backing we just retired so we do not |
| 57 | + // write through a null map. This reraces this one update but the data |
| 58 | + // is already there, and it avoids a crash. |
| 59 | + m_ctx->setLastError("ore: D3D12 buffer backing allocation failed; " |
| 60 | + "reusing in flight backing for this update"); |
| 61 | + Backing& old = m_retired.back(); |
| 62 | + m_d3dBuffer = std::move(old.resource); |
| 63 | + m_d3dMappedPtr = old.mapped; |
| 64 | + m_retired.pop_back(); |
| 65 | + return false; // Kept the current backing; retry orphaning next update. |
| 66 | + } |
| 67 | + |
| 68 | + m_currentSerial = 0; |
| 69 | + // Carry contents forward so a partial update keeps untouched bytes. Skip |
| 70 | + // when this update covers the whole buffer, and skip the self copy when the |
| 71 | + // reused backing is the one we just retired. |
| 72 | + if (!(writeOffset == 0 && writeSize == m_size) && |
| 73 | + m_d3dMappedPtr != oldMapped) |
| 74 | + memcpy(m_d3dMappedPtr, oldMapped, m_size); |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
21 | 78 | void BufferD3D12::update(const void* data, uint32_t size, uint32_t offset) |
22 | 79 | { |
23 | 80 | assert(offset + size <= m_size); |
24 | 81 | assert(m_d3dBuffer != nullptr); |
25 | 82 | assert(m_d3dMappedPtr != nullptr); |
26 | 83 |
|
27 | | - // UPLOAD heap buffer is persistently mapped — write directly. |
| 84 | + if (m_boundSinceUpdate) |
| 85 | + { |
| 86 | + // On allocation failure keep the current backing and retry next update. |
| 87 | + if (acquireFreshBacking(offset, size)) |
| 88 | + m_boundSinceUpdate = false; |
| 89 | + } |
| 90 | + // UPLOAD heap buffer is persistently mapped so write directly. |
28 | 91 | memcpy(static_cast<uint8_t*>(m_d3dMappedPtr) + offset, data, size); |
29 | 92 | } |
30 | 93 |
|
|
0 commit comments