summaryrefslogtreecommitdiff
path: root/make_ktxes.sh
blob: 2bccd514eafac429864ad08535ab7d0029439f3c (plain)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/sh
# This script creates all the KTXs necessary to test the ASTC functionality in HW.

#GL_COMPRESSED_RGBA_ASTC_4x4_KHR   0x93B0
#GL_COMPRESSED_RGBA_ASTC_5x4_KHR   0x93B1
#GL_COMPRESSED_RGBA_ASTC_5x5_KHR   0x93B2
#GL_COMPRESSED_RGBA_ASTC_6x5_KHR   0x93B3
#GL_COMPRESSED_RGBA_ASTC_6x6_KHR   0x93B4
#GL_COMPRESSED_RGBA_ASTC_8x5_KHR   0x93B5
#GL_COMPRESSED_RGBA_ASTC_8x6_KHR   0x93B6
#GL_COMPRESSED_RGBA_ASTC_8x8_KHR   0x93B7
#GL_COMPRESSED_RGBA_ASTC_10x5_KHR  0x93B8
#GL_COMPRESSED_RGBA_ASTC_10x6_KHR  0x93B9
#GL_COMPRESSED_RGBA_ASTC_10x8_KHR  0x93BA
#GL_COMPRESSED_RGBA_ASTC_10x10_KHR 0x93BB
#GL_COMPRESSED_RGBA_ASTC_12x10_KHR 0x93BC
#GL_COMPRESSED_RGBA_ASTC_12x12_KHR 0x93BD
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR 0x93D1
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR 0x93D3
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR 0x93D7
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR 0x93D9
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR 0x93DA
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR 0x93DB
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR 0x93DC
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR 0x93DD
#GL_COMPRESSED_RGBA_ASTC_3x3x3_OES 0x93C0
#GL_COMPRESSED_RGBA_ASTC_4x3x3_OES 0x93C1
#GL_COMPRESSED_RGBA_ASTC_4x4x3_OES 0x93C2
#GL_COMPRESSED_RGBA_ASTC_4x4x4_OES 0x93C3
#GL_COMPRESSED_RGBA_ASTC_5x4x4_OES 0x93C4
#GL_COMPRESSED_RGBA_ASTC_5x5x4_OES 0x93C5
#GL_COMPRESSED_RGBA_ASTC_5x5x5_OES 0x93C6
#GL_COMPRESSED_RGBA_ASTC_6x5x5_OES 0x93C7
#GL_COMPRESSED_RGBA_ASTC_6x6x5_OES 0x93C8
#GL_COMPRESSED_RGBA_ASTC_7x6x6_OES 0x93C9
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES 0x93E0
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES 0x93E1
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES 0x93E2
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES 0x93E3
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES 0x93E4
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES 0x93E5
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES 0x93E6
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES 0x93E7
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES 0x93E8
#GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES 0x93E9

ASTC_2D_Enum=(
	"0x93B0"
	"0x93B1"
	"0x93B2"
	"0x93B3"
	"0x93B4"
	"0x93B5"
	"0x93B6"
	"0x93B7"
	"0x93B8"
	"0x93B9"
	"0x93BA"
	"0x93BB"
	"0x93BC"
	"0x93BD"
	"0x93D0"
	"0x93D1"
	"0x93D2"
	"0x93D3"
	"0x93D4"
	"0x93D5"
	"0x93D6"
	"0x93D7"
	"0x93D8"
	"0x93D9"
	"0x93DA"
	"0x93DB"
	"0x93DC"
	"0x93DD"
)

Fmt=(
	"hdr"
	"ldrl"
	"ldrs"
)

Dims=(
	"2D"
	"3D"
)

Blk=(
	"4x4"
	"5x4"
	"5x5"
	"6x5"
	"6x6"
	"8x5"
	"8x6"
	"8x8"
	"10x5"
	"10x6"
	"10x8"
	"10x10"
	"12x10"
	"12x12"
)

# Executables
encoder=./astc-encoder/Binary/Linux32/astcenc
encoder=./astc-encoder/Source/astcenc
ktx_fix=./fix_mali_ktx
ktx_gen=./gen_ktx
mip_gen=./gen_mipmaps
arr_gen=./gen_array

# Global variables
compressed_dir=../compressed
decompressed_dir=../decompressed
failed=0

# Input-based variables
inFileHead=${1%%.*}
inFileExt=${1##$inFileHead}
img_w=`identify -format "%[fx:w]" $1`
img_h=`identify -format "%[fx:h]" $1`
format=${2}
max_dim=$(( $img_w < $img_h ? $img_h : $img_w ))
MAX_LEVEL=`echo "a=l($max_dim)/l(2); scale=0; a/1" | bc -l`

# Function definitions
function run_cmd {
   cmd=$1
   output=$2
   eval $cmd
   if [ ! -e "$output" ]; then
      echo $cmd
      echo "File not created: $output"
      failed=1
   fi
}

function minify {
	img_w=$1
	if [ $(($img_w >> 1)) -eq 0 ]; then
		img_w=1
	else
		img_w=$(($img_w >> 1))
	fi
	echo $img_w
}

# Generate mipmapped KTXs for each configuration combination:
# * HDR, LDR-SRGB, LDR-LINEAR
# * Blocks from 4x4 to 12x12
# * LODs specific to starting image size
# * Compressed vs decompressed
function create_ktx_for_fmt {
	i=$1
	switch="" # HDR by default
	decopts=""
	encopts="-veryfast"
	astc_fmt_array_offset=0
	if [ "$i" = "ldrs" ]; then
		switch="s"
		astc_fmt_array_offset=14
	elif [ "$i" = "ldrl" ]; then
		switch="l"
	else
		decopts+=" -hdr"
		encopts+=" -hdr"
	fi

	# Generate all the block configurations.
	for n in $(seq 0 13); do
		blk=${Blk[$n]}
		outFileHead=${inFileHead}-$blk
		outDirC=$compressed_dir/$i/$blk
		outDirD=$decompressed_dir/$i/$blk
		mkdir -p $outDirD $outDirC

		# Generate all the Level of Details
		for lod in $(seq 0 $MAX_LEVEL); do

			# Choose the right base file
			inFile=${inFileHead}-${lod}$inFileExt

			# Setup output files
			outFile=${outFileHead}-$lod
			outFileA=$outDirC/${outFile}.astc
			outFileCK=$outDirC/${outFile}.ktx
			outFileK=$outDirD/${outFile}.ktx

			# Create an ASTC from the input file.
         run_cmd "$encoder -c$switch $inFile $outFileA $blk $encopts > /dev/null" $outFileA
         if [ $failed -eq 1 ]; then
            break
         fi

			# Create a KTX from the ASTC.
         run_cmd "$encoder -d$switch $outFileA $outFileK $decopts > /dev/null" $outFileK
         if [ $failed -eq 1 ]; then
            break
         fi

			# Wrap the compressed ASTC in a KTX.
         run_cmd "$ktx_gen $outFileA $outFileCK ${ASTC_2D_Enum[($n + $astc_fmt_array_offset)]} > /dev/null" $outFileCK
         if [ $failed -eq 1 ]; then
            break
         fi

			# Delete the ASTC.
			rm $outFileA
		done

		# Generate a mipmap from all the miplevel KTXs.
		if [ $failed -eq 0 ]; then
         run_cmd "$mip_gen $outDirC/ ../${outFileHead}.ktx" $outDirC/../${outFileHead}.ktx
         if [ $failed -eq 1 ]; then
				rm $outDirC/${outFileHead}.ktx
         fi

         run_cmd "$mip_gen $outDirD/ ../${outFileHead}.ktx" $outDirD/../${outFileHead}.ktx
         if [ $failed -eq 1 ]; then
				rm $outDirD/${outFileHead}.ktx
         fi

			# Mali-decompressed sRGB & HDR ASTCs have the wrong glInternalFormat; correct this.
			if [ "$i" = "hdr" -o "$i" = "ldrs" ]; then
				$ktx_fix $outDirD/../${outFileHead}.ktx > /dev/null
				#echo "$ktx_fix $outDirD/../${outFileHead}.ktx"
			fi

			# Delete the miplevels KTXs.
			rm -rf $outDirC $outDirD
		fi
	done

   # Generate the 2D texture array
   run_cmd "$arr_gen $decompressed_dir/$i/${inFileHead}.ktx $MAX_LEVEL `ls -vd $decompressed_dir/$i/* | grep ktx`" $decompressed_dir/$i/${inFileHead}.ktx
   if [ $failed -eq 1 ]; then
      rm $outDirD/${inFileHead}.ktx
   fi
}


# MAIN
# Generate all miplevels from the source image
for lod in $(seq 0 $MAX_LEVEL); do

	# Resize the image for current level of detail (LOD).
   # Change the hue of each miplevel
	lod_out=${inFileHead}-${lod}$inFileExt
   percent=$( echo "scale = 4; a = $lod*200/$MAX_LEVEL + 100; if (a < 200) a else a - 200" | bc )
	convert -modulate 100,100,$percent ${inFileHead}$inFileExt"[${img_w}x${img_h}!]" $lod_out
	echo "LOD-$lod dimensions are: ${img_w}x${img_h}"

	# Generate next LOD dimensions.
	img_w=$(minify $img_w)
	img_h=$(minify $img_h)
done


# Generate a specific format or all formats
if [ -n "$format" ]; then
	create_ktx_for_fmt $format
else 
	for i in ${Fmt[@]}; do
	       create_ktx_for_fmt $i
	done
fi

# Delete each level of detail
for lod in $(seq 0 $MAX_LEVEL); do
	rm ${inFileHead}-${lod}$inFileExt
done