29#include "NvFlowContext.h"
30#include "NvFlowArray.h"
34 NvFlowBuffer* buffer =
nullptr;
35 NvFlowUint64 bufferSizeInBytes = 0llu;
36 NvFlowBool32 isActive = NV_FLOW_FALSE;
37 NvFlowUint64 completedFrame = ~0llu;
38 NvFlowUint64 completedGlobalFrame = ~0llu;
39 NvFlowUint64 version = ~0llu;
40 NvFlowUint64 validNumBytes = 0llu;
46 NvFlowUint64 versionCounter = 0llu;
54 ptr->contextInterface = contextInterface;
59 for (NvFlowUint idx = 0u; idx < ptr->buffers.size; idx++)
61 if (ptr->buffers[idx].buffer)
63 ptr->contextInterface->destroyBuffer(
context, ptr->buffers[idx].buffer);
64 ptr->buffers[idx].buffer =
nullptr;
67 ptr->buffers.size = 0u;
73 NvFlowUint64 numBytes;
79 NvFlowUint64 bufferIdx = 0u;
80 for (; bufferIdx < ptr->buffers.size; bufferIdx++)
82 if (!ptr->buffers[bufferIdx].isActive)
87 if (bufferIdx == ptr->buffers.size)
89 bufferIdx = ptr->buffers.allocateBack();
94 if (inst->buffer && inst->bufferSizeInBytes < numBytes)
96 ptr->contextInterface->destroyBuffer(
context, inst->buffer);
97 inst->buffer =
nullptr;
98 inst->bufferSizeInBytes = 0llu;
103 bufDesc.usageFlags = eNvFlowBufferUsage_bufferCopyDst;
104 bufDesc.format = eNvFlowFormat_unknown;
105 bufDesc.structureStride = 0u;
106 bufDesc.sizeInBytes = 65536u;
107 while (bufDesc.sizeInBytes < numBytes)
109 bufDesc.sizeInBytes *= 2u;
112 inst->buffer = ptr->contextInterface->createBuffer(
context, eNvFlowMemoryType_readback, &bufDesc);
113 inst->bufferSizeInBytes = bufDesc.sizeInBytes;
117 ptr->versionCounter++;
118 inst->isActive = NV_FLOW_TRUE;
119 inst->completedFrame = ptr->contextInterface->getCurrentFrame(
context);
120 inst->completedGlobalFrame = ptr->contextInterface->getCurrentGlobalFrame(
context);
121 inst->version = ptr->versionCounter;
122 inst->validNumBytes = numBytes;
125 *pOutVersion = inst->version;
129 NvFlowBufferTransient* dst = ptr->contextInterface->registerBufferAsTransient(
context, inst->buffer);
130 for (NvFlowUint copyRangeIdx = 0u; copyRangeIdx < copyRangeCount; copyRangeIdx++)
133 copyParams.srcOffset = copyRanges[copyRangeIdx].offset;
134 copyParams.dstOffset = copyRanges[copyRangeIdx].offset;
135 copyParams.numBytes = copyRanges[copyRangeIdx].numBytes;
136 copyParams.src = src;
137 copyParams.dst = dst;
139 copyParams.debugLabel =
"ReadbackBufferCopy";
141 ptr->contextInterface->addPassCopyBuffer(
context, ©Params);
143 if (copyRangeCount == 0u)
146 copyParams.srcOffset = 0llu;
147 copyParams.dstOffset = 0llu;
148 copyParams.numBytes = 0llu;
149 copyParams.src = src;
150 copyParams.dst = dst;
152 copyParams.debugLabel =
"ReadbackBufferCopy";
154 ptr->contextInterface->addPassCopyBuffer(
context, ©Params);
158 ptr->activeBuffers.pushBack(bufferIdx);
161NV_FLOW_INLINE
void NvFlowReadbackBuffer_copy(NvFlowContext*
context,
NvFlowReadbackBuffer* ptr, NvFlowUint64 numBytes, NvFlowBufferTransient* src, NvFlowUint64* pOutVersion)
164 NvFlowReadbackBuffer_copyN(
context, ptr, numBytes, src, ©Range, 1u, pOutVersion);
170 NvFlowUint completedCount = 0u;
171 NvFlowUint64 lastFenceCompleted = ptr->contextInterface->getLastFrameCompleted(
context);
172 for (NvFlowUint activeBufferIdx = 0u; activeBufferIdx < ptr->activeBuffers.size; activeBufferIdx++)
174 if (ptr->buffers[ptr->activeBuffers[activeBufferIdx]].completedFrame > lastFenceCompleted)
180 NvFlowUint popCount = completedCount >= 2u ? completedCount - 1u : 0u;
183 for (NvFlowUint activeBufferIdx = 0u; activeBufferIdx < popCount; activeBufferIdx++)
185 ptr->buffers[ptr->activeBuffers[activeBufferIdx]].isActive = NV_FLOW_FALSE;
188 for (NvFlowUint activeBufferIdx = popCount; activeBufferIdx < ptr->activeBuffers.size; activeBufferIdx++)
190 ptr->activeBuffers[activeBufferIdx - popCount] = ptr->activeBuffers[activeBufferIdx];
192 ptr->activeBuffers.size = ptr->activeBuffers.size - popCount;
198 return (NvFlowUint)ptr->activeBuffers.size;
201NV_FLOW_INLINE NvFlowUint64 NvFlowReadbackBuffer_getCompletedGlobalFrame(NvFlowContext*
context,
NvFlowReadbackBuffer* ptr, NvFlowUint activeIdx)
203 if (activeIdx < ptr->activeBuffers.size)
205 return ptr->buffers[ptr->activeBuffers[activeIdx]].completedGlobalFrame;
210NV_FLOW_INLINE
void* NvFlowReadbackBuffer_map(NvFlowContext*
context,
NvFlowReadbackBuffer* ptr, NvFlowUint activeIdx, NvFlowUint64* pOutVersion, NvFlowUint64* pNumBytes)
212 if (activeIdx > ptr->activeBuffers.size)
228 *pOutVersion = inst->version;
232 *pNumBytes = inst->validNumBytes;
234 return ptr->contextInterface->mapBuffer(
context, inst->buffer);
237NV_FLOW_INLINE
void* NvFlowReadbackBuffer_mapLatest(NvFlowContext*
context,
NvFlowReadbackBuffer* ptr, NvFlowUint64* pOutVersion, NvFlowUint64* pNumBytes)
239 NvFlowReadbackBuffer_flush(
context, ptr);
241 NvFlowUint64 lastFenceCompleted = ptr->contextInterface->getLastFrameCompleted(
context);
242 bool shouldMap =
true;
243 if (ptr->activeBuffers.size > 0u)
245 if (ptr->buffers[ptr->activeBuffers[0u]].completedFrame > lastFenceCompleted)
250 else if (ptr->buffers[ptr->activeBuffers[0u]].completedFrame > lastFenceCompleted)
267 return NvFlowReadbackBuffer_map(
context, ptr, 0u, pOutVersion, pNumBytes);
272 if (activeIdx < ptr->activeBuffers.size)
275 ptr->contextInterface->unmapBuffer(
context, inst->buffer);
281 NvFlowReadbackBuffer_unmap(
context, ptr, 0u);
Definition NvFlowArray.h:36
Definition NvFlowContext.h:96
Definition NvFlowContext.h:362
Definition NvFlowContext.h:300
Definition NvFlowReadbackBuffer.h:71
Definition NvFlowReadbackBuffer.h:33
Definition NvFlowReadbackBuffer.h:44