Skip to content

Commit e198a8e

Browse files
all loaders (importers) and writers (exporters) adjusted to the new API
1 parent 9d07cc9 commit e198a8e

25 files changed

+440
-460
lines changed

include/nbl/system/ISystemFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ISystemFile : public IFile
4444
}
4545

4646
//
47-
//friend class ISystem::CAsyncQueue;
47+
friend class ISystem::CAsyncQueue;
4848
virtual size_t asyncRead(void* buffer, size_t offset, size_t sizeToRead) = 0;
4949
virtual size_t asyncWrite(const void* buffer, size_t offset, size_t sizeToWrite) = 0;
5050

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4-
54
#include "CBufferLoaderBIN.h"
65

7-
namespace nbl
6+
using namespace nbl;
7+
using namespace nbl::asset;
8+
9+
asset::SAssetBundle CBufferLoaderBIN::loadAsset(system::IFile* _file, const asset::IAssetLoader::SAssetLoadParams& _params, asset::IAssetLoader::IAssetLoaderOverride* _override, uint32_t _hierarchyLevel)
810
{
9-
namespace asset
10-
{
11-
asset::SAssetBundle CBufferLoaderBIN::loadAsset(system::IFile* _file, const asset::IAssetLoader::SAssetLoadParams& _params, asset::IAssetLoader::IAssetLoaderOverride* _override, uint32_t _hierarchyLevel)
12-
{
13-
if (!_file)
14-
return {};
11+
if (!_file)
12+
return {};
1513

16-
SContext ctx(_file->getSize());
17-
ctx.file = _file;
18-
system::future<size_t> read_future;
19-
ctx.file->read(read_future, ctx.sourceCodeBuffer->getPointer(), 0u, ctx.sourceCodeBuffer->getSize());
20-
read_future.get();
14+
SContext ctx(_file->getSize());
15+
ctx.file = _file;
2116

22-
return SAssetBundle(nullptr,{std::move(ctx.sourceCodeBuffer)});
23-
}
17+
system::IFile::success_t success;
18+
ctx.file->read(success, ctx.sourceCodeBuffer->getPointer(), 0u, ctx.sourceCodeBuffer->getSize());
19+
if (success)
20+
return {};
2421

25-
bool CBufferLoaderBIN::isALoadableFileFormat(system::IFile* _file, const system::logger_opt_ptr logger) const
26-
{
27-
return true; // validation if needed
28-
}
29-
}
22+
return SAssetBundle(nullptr,{std::move(ctx.sourceCodeBuffer)});
23+
}
24+
25+
bool CBufferLoaderBIN::isALoadableFileFormat(system::IFile* _file, const system::logger_opt_ptr logger) const
26+
{
27+
return true; // validation if needed
3028
}

src/nbl/asset/interchange/CGLILoader.cpp

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ namespace nbl
230230

231231
const auto sizeOfData = memory.size();
232232

233-
system::future<size_t> future;
234-
file->read(future, memory.data(), 0, sizeOfData);
235-
future.get();
233+
system::IFile::success_t success;
234+
file->read(success, memory.data(), 0, sizeOfData);
235+
if (!success)
236+
return false;
236237

237238
if (fileName.rfind(".dds") != std::string::npos)
238239
texture = gli::load_dds(memory.data(), sizeOfData);
@@ -246,7 +247,6 @@ namespace nbl
246247
else
247248
{
248249
logger.log("LOADING GLI: failed to load the file %s", system::ILogger::ELL_ERROR, file->getFileName().string().c_str());
249-
250250
return false;
251251
}
252252
}
@@ -259,50 +259,34 @@ namespace nbl
259259
constexpr std::array<uint8_t, 12> ktxMagic = { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };
260260
constexpr std::array<uint8_t, 16> kmgMagic = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
261261

262-
system::future<size_t> future;
262+
// TODO: try to read the headers regardless of extension
263+
system::IFile::success_t success;
263264
if (fileName.rfind(".dds") != std::string::npos)
264265
{
265266
std::remove_const<decltype(ddsMagic)>::type tmpBuffer;
266-
_file->read(future, &tmpBuffer, 0, sizeof(ddsMagic));
267-
future.get();
268-
if (*reinterpret_cast<decltype(ddsMagic)*>(&tmpBuffer) == ddsMagic)
269-
{
267+
_file->read(success, &tmpBuffer, 0, sizeof(ddsMagic));
268+
if (success && *reinterpret_cast<decltype(ddsMagic)*>(&tmpBuffer) == ddsMagic)
270269
return true;
271-
}
272270
else
273-
{
274271
logger.log("LOAD GLI: Invalid (non-DDS) file!", system::ILogger::ELL_ERROR);
275-
}
276272
}
277273
else if (fileName.rfind(".kmg") != std::string::npos)
278274
{
279275
std::remove_const<decltype(kmgMagic)>::type tmpBuffer;
280-
_file->read(future, tmpBuffer.data(), 0, sizeof(kmgMagic[0]) * kmgMagic.size());
281-
future.get();
282-
if (tmpBuffer == kmgMagic)
283-
{
276+
_file->read(success, tmpBuffer.data(), 0, sizeof(kmgMagic[0]) * kmgMagic.size());
277+
if (success && tmpBuffer==kmgMagic)
284278
return true;
285-
}
286279
else
287-
{
288280
logger.log("LOAD GLI: Invalid (non-KMG) file!", system::ILogger::ELL_ERROR);
289-
}
290-
291281
}
292282
else if (fileName.rfind(".ktx") != std::string::npos)
293283
{
294284
std::remove_const<decltype(ktxMagic)>::type tmpBuffer;
295-
_file->read(future, tmpBuffer.data(), 0, sizeof(ktxMagic[0]) * ktxMagic.size());
296-
297-
future.get();
298-
if (tmpBuffer == ktxMagic)
299-
{
285+
_file->read(success, tmpBuffer.data(), 0, sizeof(ktxMagic[0]) * ktxMagic.size());
286+
if (success && tmpBuffer==ktxMagic)
300287
return true;
301-
}
302288
else
303-
{
304289
logger.log("LOAD GLI: Invalid (non-KTX) file!", system::ILogger::ELL_ERROR);
305-
}
306290
}
307291

308292
return false;

src/nbl/asset/interchange/CGLIWriter.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,9 @@ bool performSavingAsIFile(gli::texture& texture, system::IFile* file, system::IS
227227

228228
if (properlyStatus)
229229
{
230-
system::future<size_t> future;
231-
file->write(future, memory.data(), 0, memory.size());
232-
future.get();
233-
return true;
230+
system::IFile::success_t success;
231+
file->write(success, memory.data(), 0, memory.size());
232+
return bool(success);
234233
}
235234
else
236235
{

src/nbl/asset/interchange/CGLSLLoader.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ SAssetBundle CGLSLLoader::loadAsset(system::IFile* _file, const IAssetLoader::SA
1414
if (!_file)
1515
return {};
1616

17-
1817
auto len = _file->getSize();
1918
void* source = _NBL_ALIGNED_MALLOC(len+1u,_NBL_SIMD_ALIGNMENT);
20-
system::future<size_t> future;
21-
_file->read(future, source, 0, len);
22-
future.get();
19+
20+
system::IFile::success_t success;
21+
_file->read(success, source, 0, len);
22+
if (!success)
23+
return {};
24+
2325
reinterpret_cast<char*>(source)[len] = 0;
2426

2527

src/nbl/asset/interchange/CGLSLLoader.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
44

5-
#ifndef __NBL_ASSET_C_GLSL_LOADER_H_INCLUDED__
6-
#define __NBL_ASSET_C_GLSL_LOADER_H_INCLUDED__
5+
#ifndef _NBL_ASSET_C_GLSL_LOADER_H_INCLUDED_
6+
#define _NBL_ASSET_C_GLSL_LOADER_H_INCLUDED_
77

88
#include <algorithm>
99

1010
#include "nbl/asset/interchange/IAssetLoader.h"
1111
#include <nbl/system/ISystem.h>
1212

13-
namespace nbl
14-
{
15-
namespace asset
13+
namespace nbl::asset
1614
{
1715

1816
//! Surface Loader for PNG files
1917
class CGLSLLoader final : public asset::IAssetLoader
2018
{
2119
public:
2220
CGLSLLoader() = default;
21+
2322
bool isALoadableFileFormat(system::IFile* _file, const system::logger_opt_ptr logger = nullptr) const override
2423
{
2524
char tmp[10] = { 0 };
@@ -28,16 +27,17 @@ class CGLSLLoader final : public asset::IAssetLoader
2827
size_t readPos = 0;
2928
while (readPos+sizeof(tmp)<filesize)
3029
{
31-
system::future<size_t> future;
32-
_file->read(future, tmp, readPos, sizeof tmp);
33-
size_t count = future.get();
30+
system::IFile::success_t success;
31+
_file->read(success, tmp, readPos, sizeof(tmp));
32+
if (!success)
33+
return false;
3434

3535
if (strncmp(tmp,"#version ",9u)==0)
3636
return true;
3737
auto found = std::find(tmp,end,'#');
3838
if (found==end || found==tmp)
3939
{
40-
readPos += count;
40+
readPos += sizeof(tmp);
4141
continue;
4242
}
4343
readPos += found - end;
@@ -57,8 +57,7 @@ class CGLSLLoader final : public asset::IAssetLoader
5757
asset::SAssetBundle loadAsset(system::IFile* _file, const asset::IAssetLoader::SAssetLoadParams& _params, asset::IAssetLoader::IAssetLoaderOverride* _override = nullptr, uint32_t _hierarchyLevel = 0u) override;
5858
};
5959

60-
} // namespace asset
61-
} // namespace nbl
60+
} // namespace nbl::asset
6261

6362
#endif
6463

src/nbl/asset/interchange/CGLTFLoader.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
#ifdef _NBL_COMPILE_WITH_GLTF_LOADER_
88

99
#include "nbl/asset/utils/CDerivativeMapCreator.h"
10+
1011
#include "simdjson/singleheader/simdjson.h"
1112
#include <algorithm>
13+
1214
#include "nbl/core/execution.h"
1315

16+
using namespace nbl;
17+
using namespace nbl::asset;
1418

15-
namespace nbl
16-
{
17-
namespace asset
18-
{
1919
enum WEIGHT_ENCODING
2020
{
2121
WE_UNORM8,
@@ -104,9 +104,10 @@ namespace nbl
104104

105105
auto jsonBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(_file->getSize());
106106
{
107-
system::future<size_t> future;
108-
_file->read(future, jsonBuffer->getPointer(), 0u, jsonBuffer->getSize());
109-
future.get();
107+
system::IFile::success_t success;
108+
_file->read(success, jsonBuffer->getPointer(), 0u, jsonBuffer->getSize());
109+
if (!success)
110+
return false;
110111
}
111112

112113
simdjson::dom::object tweets;
@@ -1637,9 +1638,10 @@ namespace nbl
16371638

16381639
auto jsonBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(_file->getSize());
16391640
{
1640-
system::future<size_t> future;
1641-
_file->read(future, jsonBuffer->getPointer(), 0u, jsonBuffer->getSize());
1642-
future.get();
1641+
system::IFile::success_t success;
1642+
_file->read(success, jsonBuffer->getPointer(), 0u, jsonBuffer->getSize());
1643+
if (!success)
1644+
return false;
16431645
}
16441646

16451647
simdjson::dom::object tweets = parser.parse(reinterpret_cast<uint8_t*>(jsonBuffer->getPointer()), jsonBuffer->getSize());
@@ -2429,7 +2431,4 @@ namespace nbl
24292431
return true;
24302432
}
24312433

2432-
}
2433-
}
2434-
24352434
#endif // _NBL_COMPILE_WITH_GLTF_LOADER_

src/nbl/asset/interchange/CImageLoaderJPG.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,9 @@ bool CImageLoaderJPG::isALoadableFileFormat(system::IFile* _file, const system::
157157
return false;
158158

159159
uint32_t header = 0;
160-
system::future<size_t> future;
161-
_file->read(future, &header, 6, sizeof(uint32_t));
162-
future.get();
163-
return (header&0x00FFD8FFu)==0x00FFD8FFu;
160+
system::IFile::success_t success;
161+
_file->read(success, &header, 6, sizeof(uint32_t));
162+
return success && (header&0x00FFD8FFu)==0x00FFD8FFu;
164163
#endif
165164
}
166165

@@ -178,9 +177,10 @@ asset::SAssetBundle CImageLoaderJPG::loadAsset(system::IFile* _file, const asset
178177

179178
uint8_t* input = new uint8_t[_file->getSize()];
180179

181-
system::future<size_t> future;
182-
_file->read(future, input, 0, _file->getSize());
183-
future.get();
180+
system::IFile::success_t success;
181+
_file->read(success, input, 0, _file->getSize());
182+
if (!success)
183+
return {};
184184

185185
// allocate and initialize JPEG decompression object
186186
struct jpeg_decompress_struct cinfo;

src/nbl/asset/interchange/CImageLoaderOpenEXR.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@ namespace nbl
7272

7373
virtual bool read(char c[/*n*/], int n) override
7474
{
75-
system::future<size_t> future;
75+
system::ISystem::future_t<size_t> future;
7676
nblFile->read(future, c, fileOffset, n);
77-
const auto bytesRead = future.get();
78-
fileOffset += bytesRead;
77+
fileOffset += future.get();
7978

80-
return true;
79+
return future.get()==static_cast<size_t>(n);
8180
}
8281

8382
//--------------------------------------------------------
@@ -410,10 +409,9 @@ namespace nbl
410409
bool CImageLoaderOpenEXR::isALoadableFileFormat(system::IFile* _file, const system::logger_opt_ptr logger) const
411410
{
412411
char magicNumberBuffer[sizeof(SContext::magicNumber)];
413-
system::future<size_t> future;
414-
_file->read(future, magicNumberBuffer, 0, sizeof(SContext::magicNumber));
415-
future.get();
416-
return isImfMagic(magicNumberBuffer);
412+
system::IFile::success_t success;
413+
_file->read(success, magicNumberBuffer, 0, sizeof(SContext::magicNumber));
414+
return success && isImfMagic(magicNumberBuffer);
417415
}
418416

419417
template<typename rgbaFormat>

src/nbl/asset/interchange/CImageLoaderPNG.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void PNGAPI user_read_data_fcn(png_structp png_pt, png_bytep data, png_size_t le
6161

6262
system::IFile* file=(system::IFile*)png_get_io_ptr(png_pt);
6363

64-
system::future<size_t> future;
64+
system::ISystem::future_t<size_t> future;
6565
file->read(future, data, file_pos, length);
6666
check = future.get();
6767
file_pos += length;
@@ -83,13 +83,12 @@ bool CImageLoaderPng::isALoadableFileFormat(system::IFile* _file, const system::
8383

8484
png_byte buffer[8];
8585

86-
system::future<size_t> future;
87-
_file->read(future, buffer, 0, 8);
8886
// Read the first few bytes of the PNG _file
89-
if (future.get() != 8)
90-
{
87+
system::IFile::success_t success;
88+
_file->read(success, buffer, 0, sizeof(buffer));
89+
if (!success)
9190
return false;
92-
}
91+
9392
// Check if it really is a PNG _file
9493
return !png_sig_cmp(buffer, 0, 8);
9594
#else
@@ -114,9 +113,9 @@ asset::SAssetBundle CImageLoaderPng::loadAsset(system::IFile* _file, const asset
114113
png_byte buffer[8];
115114
// Read the first few bytes of the PNG _file
116115

117-
system::future<size_t> future;
118-
_file->read(future, buffer, 0, sizeof buffer);
119-
if(future.get() != 8 )
116+
system::IFile::success_t success;
117+
_file->read(success, buffer, 0, sizeof(buffer));
118+
if (!success)
120119
{
121120
_params.logger.log("LOAD PNG: can't read _file\n", system::ILogger::ELL_ERROR, _file->getFileName().string());
122121
return {};

0 commit comments

Comments
 (0)