RavEngine
Loading...
Searching...
No Matches
NvFlowDynamicBuffer.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2014-2022 NVIDIA Corporation. All rights reserved.
26
27#pragma once
28
29#include "NvFlowContext.h"
30#include "NvFlowArray.h"
31
33{
34 NvFlowContextInterface* contextInterface = nullptr;
35 NvFlowBufferUsageFlags flags = 0u;
36 NvFlowFormat format = eNvFlowFormat_unknown;
37 NvFlowUint structureStride = 0u;
38
39 NvFlowBuffer* deviceBuffer = nullptr;
40 NvFlowUint64 deviceNumBytes = 0llu;
41
42 NvFlowBufferTransient* transientBuffer = nullptr;
43 NvFlowUint64 transientFrame = ~0llu;
44};
45
46NV_FLOW_INLINE void NvFlowDynamicBuffer_init(NvFlowContextInterface* contextInterface, NvFlowContext* context, NvFlowDynamicBuffer* ptr, NvFlowBufferUsageFlags flags, NvFlowFormat format, NvFlowUint structureStride)
47{
48 ptr->contextInterface = contextInterface;
49
50 ptr->flags = flags;
51 ptr->format = format;
52 ptr->structureStride = structureStride;
53}
54
55NV_FLOW_INLINE void NvFlowDynamicBuffer_destroy(NvFlowContext* context, NvFlowDynamicBuffer* ptr)
56{
57 if (ptr->deviceBuffer)
58 {
59 ptr->contextInterface->destroyBuffer(context, ptr->deviceBuffer);
60 ptr->deviceBuffer = nullptr;
61 }
62}
63
64NV_FLOW_INLINE void NvFlowDynamicBuffer_resize(NvFlowContext* context, NvFlowDynamicBuffer* ptr, NvFlowUint64 numBytes)
65{
66 if (ptr->deviceBuffer && ptr->deviceNumBytes < numBytes)
67 {
68 ptr->contextInterface->destroyBuffer(context, ptr->deviceBuffer);
69 ptr->deviceBuffer = nullptr;
70 ptr->deviceNumBytes = 0llu;
71 ptr->transientFrame = ~0llu;
72 }
73 if (!ptr->deviceBuffer)
74 {
75 NvFlowBufferDesc bufDesc = {};
76 bufDesc.format = ptr->format;
77 bufDesc.usageFlags = ptr->flags;
78 bufDesc.structureStride = ptr->structureStride;
79 bufDesc.sizeInBytes = 65536u;
80 while (bufDesc.sizeInBytes < numBytes)
81 {
82 bufDesc.sizeInBytes *= 2u;
83 }
84
85 ptr->deviceNumBytes = bufDesc.sizeInBytes;
86 ptr->deviceBuffer = ptr->contextInterface->createBuffer(context, eNvFlowMemoryType_device, &bufDesc);
87 }
88}
89
90NV_FLOW_INLINE NvFlowBufferTransient* NvFlowDynamicBuffer_getTransient(NvFlowContext* context, NvFlowDynamicBuffer* ptr)
91{
92 if (ptr->transientFrame == ptr->contextInterface->getCurrentFrame(context))
93 {
94 return ptr->transientBuffer;
95 }
96 if (ptr->deviceBuffer)
97 {
98 ptr->transientBuffer = ptr->contextInterface->registerBufferAsTransient(context, ptr->deviceBuffer);
99 ptr->transientFrame = ptr->contextInterface->getCurrentFrame(context);
100 }
101 return ptr->transientBuffer;
102}
Definition base.h:1940
Definition NvFlowContext.h:96
Definition NvFlowContext.h:362
Definition NvFlowDynamicBuffer.h:33