15#if !defined(GPURECONSTRUCTIONIO_H)
16#define GPURECONSTRUCTIONIO_H
37 throw std::bad_alloc();
44 GPUError(
"Error registering memory for GPU: %p - %zu bytes\n", (
void*)u.get(),
n *
sizeof(T));
45 throw std::bad_alloc();
53template <
class T,
class S>
57 uint32_t numTotal = 0;
58 for (int32_t
i = 0;
i <
count;
i++) {
65 for (int32_t
i = 0;
i <
count;
i++) {
66 fwrite(&
num[
i],
sizeof(
num[
i]), 1, fp);
68 fwrite(entries[
i],
sizeof(*entries[
i]),
num[
i], fp);
77template <
class T,
class S>
84 size_t r,
pos = ftell(fp);
85 r = fread(&inType,
sizeof(inType), 1, fp);
86 if (
r != 1 || inType !=
type) {
87 fseek(fp,
pos, SEEK_SET);
93 for (int32_t
i = 0;
i <
count;
i++) {
94 r = fread(&
num[
i],
sizeof(
num[
i]), 1, fp);
100 r = fread(
m,
sizeof(*entries[
i]),
num[
i], fp);
114 FILE* fp = fopen(file,
"w+b");
118 size_t size[2] = {
sizeof(*obj), obj->getFlatBufferSize()};
119 fwrite(
size,
sizeof(
size[0]), 2, fp);
120 fwrite(obj, 1,
size[0], fp);
121 fwrite(obj->getFlatBufferPtr(), 1,
size[1], fp);
128 FILE* fp = fopen(file,
"rb");
132 size_t size[2] = {0},
r;
134 if (
r == 0 ||
size[0] !=
sizeof(T)) {
136 GPUError(
"ERROR reading %s, invalid size: %zu (%zu expected)", file,
size[0],
sizeof(T));
137 throw std::runtime_error(
"invalid size");
139 std::unique_ptr<T>
retVal(
new T);
146 GPUInfo(
"Read %zu bytes from %s",
r, file);
148 retVal->clearInternalBufferPtr();
157 FILE* fp = fopen(file,
"w+b");
161 size_t size =
sizeof(*obj);
163 fwrite(obj, 1,
size, fp);
170 FILE* fp = fopen(file,
"rb");
172 if (errorOnMissing) {
173 *errorOnMissing =
true;
179 if (
r == 0 || (!allowSmaller &&
size !=
sizeof(T))) {
181 GPUError(
"ERROR reading %s, invalid size: %zu (%zu expected)", file,
size,
sizeof(T));
182 throw std::runtime_error(
"invalid size");
184 std::unique_ptr<T>
retVal(
nullptr);
185 if (obj ==
nullptr) {
186 retVal = std::make_unique<T>();
189 r = fread(obj, 1,
size, fp);
192 GPUError(
"ERROR reading %s, read %zu (%zu expected)", file,
r,
size);
193 throw std::runtime_error(
"invalid size");
196 GPUInfo(
"Read %zu bytes from %s",
r, file);
198 if (errorOnMissing) {
199 *errorOnMissing =
false;
207 FILE* fp = fopen(file,
"w+b");
211 size_t size =
sizeof(*obj);
213 fwrite(&dynamicSize,
sizeof(dynamicSize), 1, fp);
214 fwrite(obj, 1, dynamicSize, fp);
218template <
class T, auto F>
221 FILE* fp = fopen(file,
"rb");
225 size_t size, dynsize,
r, r2;
227 r2 = fread(&dynsize,
sizeof(dynsize), 1, fp);
228 if (
r == 0 || r2 == 0 ||
size !=
sizeof(T) || dynsize <
size) {
230 GPUError(
"ERROR reading %s, invalid size: %zu (%zu buffer size, %zu object size expected)", file,
size, dynsize,
sizeof(T));
231 throw std::runtime_error(
"invalid size");
233 std::unique_ptr<T> tmp = std::make_unique<T>();
234 r = fread(tmp.get(),
sizeof(T), 1, fp);
237 GPUError(
"ERROR reading %s %zu (%zu expected)", file,
size,
sizeof(T));
238 throw std::runtime_error(
"read error");
240 if ((tmp.get()->*F)() != dynsize) {
242 GPUError(
"ERROR in %s: invalid size: %zu (%zu expected)", file, dynsize, (tmp.get()->*F)());
243 throw std::runtime_error(
"invalid size");
246 memcpy(newObj.
get(), tmp.get(),
sizeof(T));
247 r = fread(newObj.
getraw() +
sizeof(T), 1, dynsize -
sizeof(T), fp);
249 if (
r != dynsize -
sizeof(T)) {
250 GPUError(
"ERROR in %s: File Read error in %s: %zu (%zu expected)", file,
r, dynsize);
251 throw std::runtime_error(
"invalid size");
254 GPUInfo(
"Read %zu bytes from %s",
r + dynsize, file);
static void computePointerWithAlignment(T *&basePtr, S *&objPtr, size_t nEntries=1)
std::unique_ptr< T > ReadStructFromFile(const char *file, T *obj=nullptr, bool *errorOnMissing=nullptr, bool allowSmaller=false)
static uint32_t getNIOTypeMultiplicity(InOutPointerType type)
GPUOutputControl mInputControl
uint32_t DumpData(FILE *fp, const T *const *entries, const S *num, InOutPointerType type)
std::unique_ptr< T > ReadFlatObjectFromFile(const char *file)
void DumpStructToFile(const T *obj, const char *file)
void DumpDynamicStructToFile(const T *obj, size_t dynamicSize, const char *file)
T * AllocateIOMemoryHelper(size_t n, const T *&ptr, std::unique_ptr< T[]> &u)
static constexpr const char *const IOTYPENAMES[]
size_t ReadData(FILE *fp, const T **entries, S *num, std::unique_ptr< T[]> *mem, InOutPointerType type, T **nonConstPtrs=nullptr)
const GPUSettingsProcessing & GetProcessingSettings() const
void DumpFlatObjectToFile(const T *obj, const char *file)
int32_t registerMemoryForGPU(const void *ptr, size_t size)
aligned_unique_buffer_ptr< T > ReadDynamicStructFromFile(const char *file)
GLint GLint GLsizei GLint GLenum GLenum type
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLenum GLuint GLenum GLsizei const GLchar * buf