RavEngine
Loading...
Searching...
No Matches
NvFlowArrayBuffer.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
28#pragma once
29
30#include "NvFlowUploadBuffer.h"
31
32#include <string.h>
33
35{
36 const void* data;
37 NvFlowUint64 elementCount;
38 NvFlowUint64 version;
39};
40
42{
43 NvFlowBool32 isDirty;
44
45 NvFlowUint64 elementCount;
46 NvFlowUint64 version;
47 NvFlowUint64 firstElement;
48};
49
51{
52 NvFlowUploadBuffer uploadBuffer = {};
53
56 NvFlowUint64 totalSizeInBytes = 0llu;
57};
58
59NV_FLOW_INLINE void NvFlowArrayBuffer_init_custom(
60 NvFlowContextInterface* contextInterface,
61 NvFlowContext* context,
63 NvFlowBufferUsageFlags flags,
64 NvFlowFormat format,
65 NvFlowUint structureStride,
66 NvFlowBuffer*(NV_FLOW_ABI* createBuffer)(NvFlowContext* context, NvFlowMemoryType memoryType, const NvFlowBufferDesc* desc, void* userdata),
67 void(NV_FLOW_ABI* addPassCopyBuffer)(NvFlowContext* context, const NvFlowPassCopyBufferParams* params, void* userdata),
68 void* userdata
69)
70{
71 NvFlowUploadBuffer_init_custom(contextInterface, context, &ptr->uploadBuffer, flags, format, structureStride, createBuffer, addPassCopyBuffer, userdata);
72}
73
74NV_FLOW_INLINE NvFlowBuffer* NvFlowArrayBuffer_createBuffer(NvFlowContext* context, NvFlowMemoryType memoryType, const NvFlowBufferDesc* desc, void* userdata)
75{
76 NvFlowArrayBuffer* ptr = (NvFlowArrayBuffer*)userdata;
77 return ptr->uploadBuffer.contextInterface->createBuffer(context, memoryType, desc);
78}
79
80NV_FLOW_INLINE void NvFlowArrayBuffer_addPassCopyBuffer(NvFlowContext* context, const NvFlowPassCopyBufferParams* params, void* userdata)
81{
82 NvFlowArrayBuffer* ptr = (NvFlowArrayBuffer*)userdata;
83 ptr->uploadBuffer.contextInterface->addPassCopyBuffer(context, params);
84}
85
86NV_FLOW_INLINE void NvFlowArrayBuffer_init(NvFlowContextInterface* contextInterface, NvFlowContext* context, NvFlowArrayBuffer* ptr, NvFlowBufferUsageFlags flags, NvFlowFormat format, NvFlowUint structureStride)
87{
88 NvFlowArrayBuffer_init_custom(contextInterface, context, ptr, flags, format, structureStride, NvFlowArrayBuffer_createBuffer, NvFlowArrayBuffer_addPassCopyBuffer, ptr);
89}
90
91NV_FLOW_INLINE void NvFlowArrayBuffer_destroy(NvFlowContext* context, NvFlowArrayBuffer* ptr)
92{
93 NvFlowUploadBuffer_destroy(context, &ptr->uploadBuffer);
94 ptr->state.size = 0u;
95 ptr->copyRanges.size = 0u;
96}
97
98NV_FLOW_INLINE NvFlowBufferTransient* NvFlowArrayBuffer_update(
99 NvFlowContext* context,
100 NvFlowArrayBuffer* ptr,
101 const NvFlowArrayBufferData* arrayDatas,
102 NvFlowUint64* outFirstElements,
103 NvFlowUint64 arrayCount,
104 NvFlowUint64* outTotalSizeInBytes,
105 const char* debugName
106)
107{
108 // if arrayCount changes, reset all state
109 bool shouldResetState = false;
110 if (ptr->state.size != arrayCount)
111 {
112 shouldResetState = true;
113 }
114 // if any array size changes, reset all state, since buffer resize might occur
115 if (!shouldResetState)
116 {
117 for (NvFlowUint64 arrayIdx = 0u; arrayIdx < arrayCount; arrayIdx++)
118 {
119 if (ptr->state[arrayIdx].elementCount != arrayDatas[arrayIdx].elementCount)
120 {
121 shouldResetState = true;
122 }
123 }
124 }
125 if (shouldResetState)
126 {
127 ptr->state.reserve(arrayCount);
128 ptr->state.size = arrayCount;
129 for (NvFlowUint64 arrayIdx = 0u; arrayIdx < arrayCount; arrayIdx++)
130 {
131 ptr->state[arrayIdx].isDirty = NV_FLOW_TRUE;
132 ptr->state[arrayIdx].elementCount = 0llu;
133 ptr->state[arrayIdx].version = 0llu;
134 ptr->state[arrayIdx].firstElement = 0llu;
135 }
136 }
137
138 // mark any array dirty if version changes
139 for (NvFlowUint64 arrayIdx = 0u; arrayIdx < arrayCount; arrayIdx++)
140 {
141 if (arrayDatas[arrayIdx].elementCount != 0u || ptr->state[arrayIdx].elementCount != 0u)
142 {
143 if (arrayDatas[arrayIdx].version == 0llu || arrayDatas[arrayIdx].version != ptr->state[arrayIdx].version)
144 {
145 ptr->state[arrayIdx].isDirty = NV_FLOW_TRUE;
146 }
147 }
148 }
149
150 NvFlowBool32 anyDirty = NV_FLOW_FALSE;
151 for (NvFlowUint64 arrayIdx = 0u; arrayIdx < arrayCount; arrayIdx++)
152 {
153 if (ptr->state[arrayIdx].isDirty)
154 {
155 anyDirty = NV_FLOW_TRUE;
156 }
157 }
158
159 // compute total size
160 NvFlowUint64 totalSizeInBytes = 0llu;
161 for (NvFlowUint arrayIdx = 0u; arrayIdx < arrayCount; arrayIdx++)
162 {
163 totalSizeInBytes += ptr->uploadBuffer.structureStride * arrayDatas[arrayIdx].elementCount;
164 }
165 NvFlowUint8* mapped = nullptr;
166 if (anyDirty)
167 {
168 mapped = (NvFlowUint8*)NvFlowUploadBuffer_map(context, &ptr->uploadBuffer, totalSizeInBytes);
169 }
170
171 // update state
172 NvFlowUint64 globalFirstElement = 0llu;
173 for (NvFlowUint arrayIdx = 0u; arrayIdx < arrayCount; arrayIdx++)
174 {
175 ptr->state[arrayIdx].elementCount = arrayDatas[arrayIdx].elementCount;
176 ptr->state[arrayIdx].version = arrayDatas[arrayIdx].version;
177 ptr->state[arrayIdx].firstElement = globalFirstElement;
178
179 globalFirstElement += ptr->state[arrayIdx].elementCount;
180 }
181
182 ptr->copyRanges.size = 0u;
183 for (NvFlowUint arrayIdx = 0u; arrayIdx < arrayCount; arrayIdx++)
184 {
185 if (ptr->state[arrayIdx].isDirty)
186 {
187 NvFlowUint64 offsetInBytes = ptr->uploadBuffer.structureStride * ptr->state[arrayIdx].firstElement;
188 NvFlowUint64 sizeInBytes = ptr->uploadBuffer.structureStride * ptr->state[arrayIdx].elementCount;
189
190 // copy to host memory
191 memcpy(mapped + offsetInBytes, arrayDatas[arrayIdx].data, sizeInBytes);
192
193 // add copy range
194 NvFlowUploadBufferCopyRange copyRange = { offsetInBytes, sizeInBytes };
195 ptr->copyRanges.pushBack(copyRange);
196 }
197
198 }
199 NvFlowBufferTransient* bufferTransient = nullptr;
200 if (anyDirty)
201 {
202 bufferTransient = NvFlowUploadBuffer_unmapDeviceN(context, &ptr->uploadBuffer, ptr->copyRanges.data, ptr->copyRanges.size, debugName);
203 }
204 else
205 {
206 bufferTransient = NvFlowUploadBuffer_getDevice(context, &ptr->uploadBuffer, totalSizeInBytes);
207 }
208 // mark all arrays as clean
209 for (NvFlowUint arrayIdx = 0u; arrayIdx < arrayCount; arrayIdx++)
210 {
211 ptr->state[arrayIdx].isDirty = NV_FLOW_FALSE;
212 }
213
214 if (outFirstElements)
215 {
216 for (NvFlowUint arrayIdx = 0u; arrayIdx < arrayCount; arrayIdx++)
217 {
218 outFirstElements[arrayIdx] = ptr->state[arrayIdx].firstElement;
219 }
220 }
221
222 ptr->totalSizeInBytes = totalSizeInBytes;
223 if (outTotalSizeInBytes)
224 {
225 *outTotalSizeInBytes = totalSizeInBytes;
226 }
227
228 return bufferTransient;
229}
Definition base.h:1940
Definition NvFlowArrayBuffer.h:35
Definition NvFlowArrayBuffer.h:42
Definition NvFlowArrayBuffer.h:51
Definition NvFlowArray.h:36
Definition NvFlowContext.h:96
Definition NvFlowContext.h:362
Definition NvFlowContext.h:300
Definition NvFlowUploadBuffer.h:235
Definition NvFlowUploadBuffer.h:112