FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
astc_encoder/main.cpp at main · niepp/astc_encoder · GitHub
niepp
/
astc_encoder
Public
Notifications
You must be signed in to change notification settings
Fork
24
Star
140
Code
Issues
5
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
astc_encoder
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
259 lines (217 loc) · 6.75 KB
Breadcrumbs
astc_encoder
/
main.cpp
Copy path
File metadata and controls
259 lines (217 loc) · 6.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#
define
_CRT_SECURE_NO_WARNINGS
#
define
_WIN32_WINNT
0x600
#
include
<
string
>
#
include
<
iostream
>
#
include
<
d3d11.h
>
#
include
<
d3dcompiler.h
>
#
pragma
comment(lib, "d3d11.lib")
#
pragma
comment(lib, "d3dcompiler.lib")
#
define
STB_IMAGE_IMPLEMENTATION
#
include
"
stb_image.h
"
#
include
"
astc_encode.h
"
#
include
"
astc_save.h
"
ID3D11Texture2D*
load_tex
(ID3D11Device* pd3dDevice,
const
char
* tex_path,
bool
bSRGB)
{
int
xsize =
0
;
int
ysize =
0
;
int
components =
0
;
stbi_set_flip_vertically_on_load
(
1
);
stbi_uc* image =
stbi_load
(tex_path, &xsize, &ysize, &components, STBI_rgb_alpha);
if
(image ==
nullptr
) {
//
if we haven't returned, it's because we failed to load the file.
printf
(
"
Failed to load image %s
\n
Reason: %s
\n
"
, tex_path,
stbi_failure_reason
());
return
nullptr
;
}
//
create texture
D3D11_TEXTURE2D_DESC
TexDesc;
TexDesc.
Width
= xsize;
//
grid size of the waves, rows
TexDesc.
Height
= ysize;
//
grid size of the waves, colums
TexDesc.
MipLevels
=
1
;
TexDesc.
ArraySize
=
1
;
TexDesc.
Format
= bSRGB ?
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
:
DXGI_FORMAT_R8G8B8A8_UNORM
;
TexDesc.
SampleDesc
.
Count
=
1
;
TexDesc.
SampleDesc
.
Quality
=
0
;
TexDesc.
Usage
=
D3D11_USAGE_DEFAULT
;
TexDesc.
BindFlags
=
D3D11_BIND_SHADER_RESOURCE
;
TexDesc.
CPUAccessFlags
=
0
;
TexDesc.
MiscFlags
=
0
;
D3D11_SUBRESOURCE_DATA
InitialData;
InitialData.
pSysMem
= image;
InitialData.
SysMemPitch
= xsize *
4
;
InitialData.
SysMemSlicePitch
= xsize * ysize *
4
;
ID3D11Texture2D* pTex =
nullptr
;
pd3dDevice->
CreateTexture2D
(&TexDesc, &InitialData, &pTex);
return
pTex;
}
HRESULT
create_device_swapchain
(
HWND
hwnd, IDXGISwapChain*& pSwapChain, ID3D11Device*& pd3dDevice, ID3D11DeviceContext*& pDeviceContext)
{
DXGI_SWAP_CHAIN_DESC
desc;
ZeroMemory
(&desc,
sizeof
(
DXGI_SWAP_CHAIN_DESC
));
desc.
BufferDesc
.
Width
=
800
;
desc.
BufferDesc
.
Height
=
600
;
desc.
BufferDesc
.
RefreshRate
.
Denominator
=
0
;
desc.
BufferDesc
.
RefreshRate
.
Numerator
=
0
;
desc.
BufferDesc
.
Format
=
DXGI_FORMAT_B8G8R8A8_UNORM
;
desc.
SampleDesc
.
Count
=
1
;
//
multisampling setting
desc.
SampleDesc
.
Quality
=
0
;
//
vendor-specific flag
desc.
BufferUsage
=
DXGI_USAGE_UNORDERED_ACCESS
;
//
DXGI_USAGE_UNORDERED_ACCESS;
desc.
BufferCount
=
1
;
desc.
OutputWindow
= hwnd;
desc.
Windowed
=
TRUE
;
desc.
SwapEffect
=
DXGI_SWAP_EFFECT_DISCARD
;
desc.
Flags
=
0
;
//
This flag adds support for surfaces with a color-channel ordering different
//
from the API default. It is required for compatibility with Direct2D.
UINT
deviceFlags =
D3D11_CREATE_DEVICE_BGRA_SUPPORT
;
#
if
defined(DEBUG) || defined(_DEBUG)
deviceFlags |=
D3D11_CREATE_DEVICE_DEBUG
;
#
endif
D3D_FEATURE_LEVEL
featureLevel =
D3D_FEATURE_LEVEL
::
D3D_FEATURE_LEVEL_11_0
;
HRESULT
hr =
D3D11CreateDeviceAndSwapChain
(
nullptr
,
D3D_DRIVER_TYPE
::
D3D_DRIVER_TYPE_HARDWARE
,
nullptr
,
deviceFlags,
0
,
0
,
D3D11_SDK_VERSION
,
&desc,
&pSwapChain,
&pd3dDevice,
&featureLevel,
&pDeviceContext);
if
(
FAILED
(hr)) {
return
hr;
}
//
Verify compute shader is supported
if
(pd3dDevice->
GetFeatureLevel
() <
D3D_FEATURE_LEVEL_11_0
)
{
D3D11_FEATURE_DATA_D3D10_X_HARDWARE_OPTIONS
hwopts = {
0
};
(
void
)pd3dDevice->
CheckFeatureSupport
(
D3D11_FEATURE_D3D10_X_HARDWARE_OPTIONS
, &hwopts,
sizeof
(hwopts));
if
(!hwopts.
ComputeShaders_Plus_RawAndStructuredBuffers_Via_Shader_4_x
)
{
pd3dDevice->
Release
();
printf
(
"
DirectCompute is not supported by this device
\n
"
);
return
-
1
;
}
}
return
hr;
}
std::string
get_file_extension
(
const
char
* file_path)
{
const
char
* ext =
strrchr
(file_path,
'
.
'
);
if
(ext !=
NULL
) {
return
std::string
(ext +
1
);
}
return
"
"
;
}
void
strip_file_extension
(std::string &file_path)
{
const
char
* str = file_path.
c_str
();
char
* ext =
const_cast
<
char
*>(
strrchr
(str,
'
.
'
));
if
(ext !=
nullptr
) {
*ext =
0
;
file_path = str;
}
}
bool
parse_cmd
(
int
argc,
char
** argv, encode_option& option)
{
auto
func_arg_value = [](
int
index,
int
argc,
char
** argv,
bool
&ret) ->
bool
{
if
(index < argc && *(argv[index]) ==
'
-
'
) {
ret =
true
;
return
true
;
}
return
false
;
};
for
(
int
i =
2
; i < argc; ++i) {
if
(argv[i] ==
std::string
(
"
-4x4
"
)) {
if
(!
func_arg_value
(i, argc, argv, option.
is4x4
)) {
return
false
;
}
}
else
if
(argv[i] ==
std::string
(
"
-6x6
"
)) {
if
(!
func_arg_value
(i, argc, argv, option.
is6x6
)) {
return
false
;
}
}
else
if
(argv[i] ==
std::string
(
"
-norm
"
)) {
if
(!
func_arg_value
(i, argc, argv, option.
is_normal_map
)) {
return
false
;
}
}
else
if
(argv[i] ==
std::string
(
"
-srgb
"
)) {
if
(!
func_arg_value
(i, argc, argv, option.
srgb
)) {
return
false
;
}
}
else
if
(argv[i] ==
std::string
(
"
-alpha
"
)) {
if
(!
func_arg_value
(i, argc, argv, option.
has_alpha
)) {
return
false
;
}
}
}
return
true
;
}
int
main
(
int
argc,
char
** argv)
{
if
(argc <
2
) {
std::cout <<
"
wrong args count
"
<< std::endl;
return
-
1
;
}
encode_option option;
if
(!
parse_cmd
(argc, argv, option)) {
std::cout <<
"
wrong args options
"
<< std::endl;
return
-
1
;
}
std::cout <<
"
encode option setting:
\n
"
<<
"
has_alpha
\t
"
<< std::boolalpha << option.
has_alpha
<< std::endl
<<
"
is 4x4 block
\t
"
<< option.
is4x4
<< std::endl
<<
"
normal map
\t
"
<< option.
is_normal_map
<< std::endl
<<
"
encode in gamma color space
\t
"
<< option.
srgb
<< std::endl;
HWND
hwnd = ::
GetDesktopWindow
();
//
setting up device
IDXGISwapChain* pSwapChain =
nullptr
;
ID3D11Device* pd3dDevice =
nullptr
;
ID3D11DeviceContext* pDeviceContext =
nullptr
;
HRESULT
hr =
create_device_swapchain
(hwnd, pSwapChain, pd3dDevice, pDeviceContext);
if
(
FAILED
(hr)) {
std::cout <<
"
init d3d failed!
"
<< std::endl;
return
hr;
}
std::string src_tex = argv[
1
];
//
shader resource view
ID3D11Texture2D* pSrcTexture =
load_tex
(pd3dDevice, src_tex.
c_str
(), option.
srgb
&& (!option.
is_normal_map
));
if
(pSrcTexture ==
nullptr
) {
std::cout <<
"
load source texture failed! [
"
<< src_tex <<
"
]
"
<< std::endl;
return
-
1
;
}
D3D11_TEXTURE2D_DESC
TexDesc;
pSrcTexture->
GetDesc
(&TexDesc);
int
TexWidth = TexDesc.
Width
;
int
TexHeight = TexDesc.
Height
;
ID3D11Buffer* pOutBuf =
encode_astc
(pd3dDevice, pDeviceContext, pSrcTexture, option);
if
(pOutBuf ==
nullptr
) {
std::cout <<
"
encode astc failed!
"
<< std::endl;
return
-
1
;
}
//
save to file
D3D11_BUFFER_DESC
sbDesc;
pOutBuf->
GetDesc
(&sbDesc);
uint32_t
bufLen = sbDesc.
ByteWidth
;
uint8_t
* pMemBuf =
new
uint8_t
[bufLen];
ZeroMemory
(pMemBuf, bufLen);
hr =
read_gpu
(pd3dDevice, pDeviceContext, pOutBuf, pMemBuf, bufLen);
if
(
FAILED
(hr)) {
std::cout <<
"
save astc failed!
"
<< std::endl;
return
-
1
;
}
std::string
dst_tex
(src_tex);
strip_file_extension
(dst_tex);
dst_tex +=
"
.astc
"
;
int
DimSize = option.
is4x4
?
4
:
6
;
save_astc
(dst_tex.
c_str
(), DimSize, DimSize, TexDesc.
Width
, TexDesc.
Height
, pMemBuf, bufLen);
delete[]
pMemBuf;
pMemBuf =
nullptr
;
std::cout <<
"
save astc to:
"
<< dst_tex << std::endl;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL