-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadTexture.h
121 lines (110 loc) · 3.78 KB
/
loadTexture.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// loadTexture.hh
//
#ifndef LZZ_loadTexture_hh
#define LZZ_loadTexture_hh
#include "bgfxh.h"
/// Note that this file adds a linking dependency on libbimg_decode(Debug/release).a. This is from bimg and is built alongside bgfx
#define LZZ_INLINE inline
namespace bgfxh
{
void internal_imageReleaseCb (void * _ptr, void * _userData);
}
namespace bgfxh
{
bgfx::TextureHandle loadTexture (BGFXH_STRING const & _filename, uint32_t const _flags);
}
namespace bgfxh
{
bgfx::TextureHandle loadTextureRaw (char const * rawData, uint32_t const rawSize, uint32_t const _flags, char const * textureDbgName = "");
}
#undef LZZ_INLINE
#endif
////////////////////////////////////////////////////////////////////////
#ifdef BGFXH_IMPL
#ifndef BGFXH_DOUBLE_GUARD_loadTexture
#define BGFXH_DOUBLE_GUARD_loadTexture
// loadTexture.cpp
//
#include <bimg/decode.h>
#define LZZ_INLINE inline
namespace bgfxh
{
void internal_imageReleaseCb (void * _ptr, void * _userData)
{
BX_UNUSED(_ptr);
bimg::ImageContainer* imageContainer = (bimg::ImageContainer*)_userData;
bimg::imageFree(imageContainer);
}
}
namespace bgfxh
{
bgfx::TextureHandle loadTexture (BGFXH_STRING const & _filename, uint32_t const _flags)
{
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
BGFXH_STRING rawData;
bgfxh::readFileRawToString(_filename, rawData);
if (!rawData.size()) return handle; // File failed to read/is empty
return loadTextureRaw (&rawData[0], rawData.size(), _flags, _filename.c_str());
}
}
namespace bgfxh
{
bgfx::TextureHandle loadTextureRaw (char const * rawData, uint32_t const rawSize, uint32_t const _flags, char const * textureDbgName)
{
// Most of this function was adapted from /examples/common/bgfx_utils.cpp, created by Branimir Karadzic.
/*
* Copyright 2011-2019 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
bx::Error bErr;
bimg::ImageContainer * imageContainer = bimg::imageParse(bgfxh::m_allocator, (const void*) rawData, rawSize, bimg::TextureFormat::Count, &bErr);
if (imageContainer) {
const bgfx::Memory* mem = bgfx::makeRef(
imageContainer->m_data
, imageContainer->m_size
, internal_imageReleaseCb
, imageContainer
);
if (imageContainer->m_cubeMap) {
handle = bgfx::createTextureCube(
uint16_t(imageContainer->m_width)
, 1 < imageContainer->m_numMips
, imageContainer->m_numLayers
, bgfx::TextureFormat::Enum(imageContainer->m_format)
, _flags
, mem
);
}
else if (1 < imageContainer->m_depth) {
handle = bgfx::createTexture3D(
uint16_t(imageContainer->m_width)
, uint16_t(imageContainer->m_height)
, uint16_t(imageContainer->m_depth)
, 1 < imageContainer->m_numMips
, bgfx::TextureFormat::Enum(imageContainer->m_format)
, _flags
, mem
);
}
else if (bgfx::isTextureValid(0, false, imageContainer->m_numLayers, bgfx::TextureFormat::Enum(imageContainer->m_format), _flags) ) {
handle = bgfx::createTexture2D(
uint16_t(imageContainer->m_width)
, uint16_t(imageContainer->m_height)
, 1 < imageContainer->m_numMips
, imageContainer->m_numLayers
, bgfx::TextureFormat::Enum(imageContainer->m_format)
, _flags
, mem
);
}
if (bgfx::isValid(handle)) {
bgfx::setName(handle, textureDbgName);
}
}
return handle;
}
}
#undef LZZ_INLINE
#endif //BGFXH_DOUBLE_GUARD_loadTexture
#endif //BGFXH_IMPL