summaryrefslogtreecommitdiff
path: root/thirdparty/etc2comp/EtcBlock4x4EncodingBits.h
blob: 4065700379ac460121a5dafc80d3c14ef742ad67 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright 2015 The Etc2Comp Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <assert.h>

namespace Etc
{

	// ################################################################################
	// Block4x4EncodingBits
	// Base class for Block4x4EncodingBits_XXXX
	// ################################################################################

	class Block4x4EncodingBits
	{
	public:

		enum class Format
		{
			UNKNOWN,
			//
			RGB8,
			RGBA8,
			R11,
			RG11,
			RGB8A1,
			//
			FORMATS
		};

		static unsigned int GetBytesPerBlock(Format a_format)
		{
			switch (a_format)
			{
			case Format::RGB8:
			case Format::R11:
			case Format::RGB8A1:
				return 8;
				break;

			case Format::RGBA8:
			case Format::RG11:
				return 16;
				break;

			default:
				return 0;
				break;
			}

		}

	};

	// ################################################################################
	// Block4x4EncodingBits_RGB8
	// Encoding bits for the RGB portion of ETC1, RGB8, RGB8A1 and RGBA8
	// ################################################################################

	class Block4x4EncodingBits_RGB8
	{
	public:

		static const unsigned int BYTES_PER_BLOCK = 8;

		inline Block4x4EncodingBits_RGB8(void)
		{
			assert(sizeof(Block4x4EncodingBits_RGB8) == BYTES_PER_BLOCK);

			for (unsigned int uiByte = 0; uiByte < BYTES_PER_BLOCK; uiByte++)
			{
				auc[uiByte] = 0;
			}

		}

		typedef struct
		{
			unsigned red2 : 4;
			unsigned red1 : 4;
			//
			unsigned green2 : 4;
			unsigned green1 : 4;
			//
			unsigned blue2 : 4;
			unsigned blue1 : 4;
			//
			unsigned flip : 1;
			unsigned diff : 1;
			unsigned cw2 : 3;
			unsigned cw1 : 3;
			//
			unsigned int selectors;
		} Individual;

		typedef struct
		{
			signed dred2 : 3;
			unsigned red1 : 5;
			//
			signed dgreen2 : 3;
			unsigned green1 : 5;
			//
			signed dblue2 : 3;
			unsigned blue1 : 5;
			//
			unsigned flip : 1;
			unsigned diff : 1;
			unsigned cw2 : 3;
			unsigned cw1 : 3;
			//
			unsigned int selectors;
		} Differential;

		typedef struct
		{
			unsigned red1b : 2;
			unsigned detect2 : 1;
			unsigned red1a : 2;
			unsigned detect1 : 3;
			//
			unsigned blue1 : 4;
			unsigned green1 : 4;
			//
			unsigned green2 : 4;
			unsigned red2 : 4;
			//
			unsigned db : 1;
			unsigned diff : 1;
			unsigned da : 2;
			unsigned blue2 : 4;
			//
			unsigned int selectors;
		} T;

		typedef struct
		{
			unsigned green1a : 3;
			unsigned red1 : 4;
			unsigned detect1 : 1;
			//
			unsigned blue1b : 2;
			unsigned detect3 : 1;
			unsigned blue1a : 1;
			unsigned green1b : 1;
			unsigned detect2 : 3;
			//
			unsigned green2a : 3;
			unsigned red2 : 4;
			unsigned blue1c : 1;
			//
			unsigned db : 1;
			unsigned diff : 1;
			unsigned da : 1;
			unsigned blue2 : 4;
			unsigned green2b : 1;
			//
			unsigned int selectors;
		} H;

		typedef struct
		{
			unsigned originGreen1 : 1;
			unsigned originRed : 6;
			unsigned detect1 : 1;
			//
			unsigned originBlue1 : 1;
			unsigned originGreen2 : 6;
			unsigned detect2 : 1;
			//
			unsigned originBlue3 : 2;
			unsigned detect4 : 1;
			unsigned originBlue2 : 2;
			unsigned detect3 : 3;
			//
			unsigned horizRed2 : 1;
			unsigned diff : 1;
			unsigned horizRed1 : 5;
			unsigned originBlue4 : 1;
			//
			unsigned horizBlue1: 1;
			unsigned horizGreen : 7;
			//
			unsigned vertRed1 : 3;
			unsigned horizBlue2 : 5;
			//
			unsigned vertGreen1 : 5;
			unsigned vertRed2 : 3;
			//
			unsigned vertBlue : 6;
			unsigned vertGreen2 : 2;
		} Planar;

		union
		{
			unsigned char auc[BYTES_PER_BLOCK];
			unsigned long int ul;
			Individual individual;
			Differential differential;
			T t;
			H h;
			Planar planar;
		};

	};

	// ################################################################################
	// Block4x4EncodingBits_A8
	// Encoding bits for the A portion of RGBA8
	// ################################################################################

	class Block4x4EncodingBits_A8
	{
	public:

		static const unsigned int BYTES_PER_BLOCK = 8;
		static const unsigned int SELECTOR_BYTES = 6;

		typedef struct
		{
			unsigned base : 8;
			unsigned table : 4;
			unsigned multiplier : 4;
			unsigned selectors0 : 8;
			unsigned selectors1 : 8;
			unsigned selectors2 : 8;
			unsigned selectors3 : 8;
			unsigned selectors4 : 8;
			unsigned selectors5 : 8;
		} Data;

		Data data;

	};

	// ################################################################################
	// Block4x4EncodingBits_R11
	// Encoding bits for the R portion of R11
	// ################################################################################

	class Block4x4EncodingBits_R11
	{
	public:

		static const unsigned int BYTES_PER_BLOCK = 8;
		static const unsigned int SELECTOR_BYTES = 6;

		typedef struct
		{
			unsigned base : 8;
			unsigned table : 4;
			unsigned multiplier : 4;
			unsigned selectors0 : 8;
			unsigned selectors1 : 8;
			unsigned selectors2 : 8;
			unsigned selectors3 : 8;
			unsigned selectors4 : 8;
			unsigned selectors5 : 8;
		} Data;

		Data data;

	};

	class Block4x4EncodingBits_RG11
	{
	public:

		static const unsigned int BYTES_PER_BLOCK = 16;
		static const unsigned int SELECTOR_BYTES = 12;

		typedef struct
		{
			//Red portion
			unsigned baseR : 8;
			unsigned tableIndexR : 4;
			unsigned multiplierR : 4;
			unsigned selectorsR0 : 8;
			unsigned selectorsR1 : 8;
			unsigned selectorsR2 : 8;
			unsigned selectorsR3 : 8;
			unsigned selectorsR4 : 8;
			unsigned selectorsR5 : 8;
			//Green portion
			unsigned baseG : 8;
			unsigned tableIndexG : 4;
			unsigned multiplierG : 4;
			unsigned selectorsG0 : 8;
			unsigned selectorsG1 : 8;
			unsigned selectorsG2 : 8;
			unsigned selectorsG3 : 8;
			unsigned selectorsG4 : 8;
			unsigned selectorsG5 : 8;
		} Data;

		Data data;

	};

}