File: texelbuffer.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (330 lines) | stat: -rw-r--r-- 9,843 bytes parent folder | download | duplicates (2)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "graphics/tinygl/gl.h"
#include "graphics/tinygl/zgl.h"
#include "graphics/tinygl/zbuffer.h"
#include "graphics/tinygl/colormasks.h"
#include "graphics/tinygl/pixelbuffer.h"
#include "graphics/tinygl/texelbuffer.h"

namespace TinyGL {

#define ZB_POINT_ST_UNIT (1 << ZB_POINT_ST_FRAC_BITS)
#define ZB_POINT_ST_FRAC_MASK (ZB_POINT_ST_UNIT - 1)

TexelBuffer::TexelBuffer(uint width, uint height, uint textureSize) {
	assert(width);
	assert(height);
	assert(textureSize);

	_width = width;
	_height = height;
	_fracTextureUnit = textureSize << ZB_POINT_ST_FRAC_BITS;
	_fracTextureMask = _fracTextureUnit - 1;
	_widthRatio = (float) width / textureSize;
	_heightRatio = (float) height / textureSize;
}

static inline uint wrap(uint wrap_mode, int coord, uint _fracTextureUnit, uint _fracTextureMask) {
	switch (wrap_mode) {
	case TGL_MIRRORED_REPEAT:
		if (coord & _fracTextureUnit)
			return _fracTextureMask - (coord & _fracTextureMask);
		return coord & _fracTextureMask;
	case TGL_CLAMP_TO_EDGE:
		if (coord < 0)
			return 0;
		if ((uint) coord > _fracTextureMask)
			return _fracTextureMask;
		return coord;
	default:
		// Fall through
	case TGL_REPEAT:
		return coord & _fracTextureMask;
	}
}

void TexelBuffer::getARGBAt(
	uint wrap_s, uint wrap_t,
	int s, int t,
	uint8 &a, uint8 &r, uint8 &g, uint8 &b
) const {
	uint x, y;
	x = wrap(wrap_s, s, _fracTextureUnit, _fracTextureMask) * _widthRatio;
	y = wrap(wrap_t, t, _fracTextureUnit, _fracTextureMask) * _heightRatio;
	getARGBAt(
		(x >> ZB_POINT_ST_FRAC_BITS) + (y >> ZB_POINT_ST_FRAC_BITS) * _width,
		x & ZB_POINT_ST_FRAC_MASK, y & ZB_POINT_ST_FRAC_MASK,
		a, r, g, b
	);
}

// Nearest: store texture in original size.
class BaseNearestTexelBuffer : public TexelBuffer {
public:
	BaseNearestTexelBuffer(const byte *buf, const Graphics::PixelFormat &format, uint width, uint height, uint textureSize);
	~BaseNearestTexelBuffer();

protected:
	byte *_buf;
	Graphics::PixelFormat _format;
};

BaseNearestTexelBuffer::BaseNearestTexelBuffer(const byte *buf, const Graphics::PixelFormat &format, uint width, uint height, uint textureSize) : TexelBuffer(width, height, textureSize), _format(format) {
	uint count = _width * _height * _format.bytesPerPixel;
	_buf = (byte *)gl_malloc(count);
	memcpy(_buf, buf, count);
}

BaseNearestTexelBuffer::~BaseNearestTexelBuffer() {
	gl_free(_buf);
}

template<uint Format, uint Type>
class NearestTexelBuffer final : public BaseNearestTexelBuffer {
public:
	NearestTexelBuffer(const byte *buf, const Graphics::PixelFormat &format, uint width, uint height, uint textureSize)
	  : BaseNearestTexelBuffer(buf, format, width, height, textureSize) {}

protected:
	void getARGBAt(
		uint pixel,
		uint, uint,
		uint8 &a, uint8 &r, uint8 &g, uint8 &b
	) const override {
		Pixel col = *(((const Pixel *)_buf) + pixel);
		_format.colorToARGBT<ColorMask>(col, a, r, g, b);
	}

	typedef ColorMasks<Format, Type> ColorMask;
	typedef typename ColorMask::PixelType Pixel;
};

template<>
class NearestTexelBuffer<TGL_RGB, TGL_UNSIGNED_BYTE> final : public BaseNearestTexelBuffer {
public:
	NearestTexelBuffer(const byte *buf, const Graphics::PixelFormat &format, uint width, uint height, uint textureSize)
	  : BaseNearestTexelBuffer(buf, format, width, height, textureSize) {}

protected:
	void getARGBAt(
		uint pixel,
		uint, uint,
		uint8 &a, uint8 &r, uint8 &g, uint8 &b
	) const override {
		byte *col = _buf + (pixel * 3);
		a = 0xff;
		r = col[0];
		g = col[1];
		b = col[2];
	}
};

TexelBuffer *createNearestTexelBuffer(const byte *buf, const Graphics::PixelFormat &pf, uint format, uint type, uint width, uint height, uint textureSize) {
	if (format == TGL_RGBA && type == TGL_UNSIGNED_BYTE) {
		return new NearestTexelBuffer<TGL_RGBA, TGL_UNSIGNED_BYTE>(
			buf, pf,
			width, height,
			textureSize
		);
	} else if (format == TGL_RGB && type == TGL_UNSIGNED_BYTE) {
		return new NearestTexelBuffer<TGL_RGB,  TGL_UNSIGNED_BYTE>(
			buf, pf,
			width, height,
			textureSize
		);
	} else if (format == TGL_RGB && type == TGL_UNSIGNED_SHORT_5_6_5) {
		return new NearestTexelBuffer<TGL_RGB,  TGL_UNSIGNED_SHORT_5_6_5>(
			buf, pf,
			width, height,
			textureSize
		);
	} else if (format == TGL_RGBA && type == TGL_UNSIGNED_SHORT_5_5_5_1) {
		return new NearestTexelBuffer<TGL_RGBA, TGL_UNSIGNED_SHORT_5_5_5_1>(
			buf, pf,
			width, height,
			textureSize
		);
	} else if (format == TGL_RGBA && type == TGL_UNSIGNED_SHORT_4_4_4_4) {
		return new NearestTexelBuffer<TGL_RGBA, TGL_UNSIGNED_SHORT_4_4_4_4>(
			buf, pf,
			width, height,
			textureSize
		);
	} else {
		error("TinyGL texture: format 0x%04x and type 0x%04x combination not supported", format, type);
	}
}

// Bilinear: each texture coordinates corresponds to the 4 original image
// pixels linear interpolation has to work on, so that they are near each
// other in CPU data cache, and a single actual memory fetch happens. This
// allows applying linear filtering at render time at a very low performance
// cost. As we expect to work on small-ish textures (512*512 ?) the 4x memory
// usage increase should be negligible.
class BilinearTexelBuffer : public TexelBuffer {
public:
	BilinearTexelBuffer(byte *buf, const Graphics::PixelFormat &format, uint width, uint height, uint textureSize);
	~BilinearTexelBuffer();

protected:
	void getARGBAt(
		uint pixel,
		uint ds, uint dt,
		uint8 &a, uint8 &r, uint8 &g, uint8 &b
	) const override;

private:
	uint32 *_texels;
};

#define A_OFFSET (0 * 4)
#define R_OFFSET (1 * 4)
#define G_OFFSET (2 * 4)
#define B_OFFSET (3 * 4)
#define P00_OFFSET 0
#define P01_OFFSET 1
#define P10_OFFSET 2
#define P11_OFFSET 3
#define PIXEL_PER_TEXEL_SHIFT 2

BilinearTexelBuffer::BilinearTexelBuffer(byte *buf, const Graphics::PixelFormat &format, uint width, uint height, uint textureSize) : TexelBuffer(width, height, textureSize) {
	const Graphics::PixelBuffer src(format, buf);

	uint pixel00_offset = 0, pixel11_offset, pixel01_offset, pixel10_offset;
	uint8 *texel8;
	uint32 *texel32;

	texel32 = _texels = (uint32 *)gl_malloc((_width * _height << PIXEL_PER_TEXEL_SHIFT) * sizeof(uint32));
	for (uint y = 0; y < _height; y++) {
		for (uint x = 0; x < _width; x++) {
			texel8 = (uint8 *)texel32;
			pixel11_offset = pixel00_offset + _width + 1;
			src.getARGBAt(
				pixel00_offset,
				*(texel8 + P00_OFFSET + A_OFFSET),
				*(texel8 + P00_OFFSET + R_OFFSET),
				*(texel8 + P00_OFFSET + G_OFFSET),
				*(texel8 + P00_OFFSET + B_OFFSET)
			);
			if ((x + 1) == _width) {
				pixel11_offset -= 1;
				pixel01_offset = pixel00_offset;
			} else
				pixel01_offset = pixel00_offset + 1;
			src.getARGBAt(
				pixel01_offset,
				*(texel8 + P01_OFFSET + A_OFFSET),
				*(texel8 + P01_OFFSET + R_OFFSET),
				*(texel8 + P01_OFFSET + G_OFFSET),
				*(texel8 + P01_OFFSET + B_OFFSET)
			);
			if ((y + 1) == _height) {
				pixel11_offset -= _width;
				pixel10_offset = pixel00_offset;
			} else
				pixel10_offset = pixel00_offset + _width;
			src.getARGBAt(
				pixel10_offset,
				*(texel8 + P10_OFFSET + A_OFFSET),
				*(texel8 + P10_OFFSET + R_OFFSET),
				*(texel8 + P10_OFFSET + G_OFFSET),
				*(texel8 + P10_OFFSET + B_OFFSET)
			);
			src.getARGBAt(
				pixel11_offset,
				*(texel8 + P11_OFFSET + A_OFFSET),
				*(texel8 + P11_OFFSET + R_OFFSET),
				*(texel8 + P11_OFFSET + G_OFFSET),
				*(texel8 + P11_OFFSET + B_OFFSET)
			);
			texel32 += 1 << PIXEL_PER_TEXEL_SHIFT;
			pixel00_offset++;
		}
	}
}

BilinearTexelBuffer::~BilinearTexelBuffer() {
	gl_free(_texels);
}

static inline int interpolate(int v00, int v01, int v10, int xf, int yf) {
	return v00 + (((v01 - v00) * xf + (v10 - v00) * yf) >> ZB_POINT_ST_FRAC_BITS);
}

void BilinearTexelBuffer::getARGBAt(
	uint pixel,
	uint ds, uint dt,
	uint8 &a, uint8 &r, uint8 &g, uint8 &b
) const {
	uint p00_offset, p01_offset, p10_offset;
	uint8 *texel = (uint8 *)(_texels + (pixel << PIXEL_PER_TEXEL_SHIFT));
	if ((ds + dt) > ZB_POINT_ST_UNIT) {
		p00_offset = P11_OFFSET;
		p10_offset = P01_OFFSET;
		p01_offset = P10_OFFSET;
		ds = ZB_POINT_ST_UNIT - ds;
		dt = ZB_POINT_ST_UNIT - dt;
	} else {
		p00_offset = P00_OFFSET;
		p10_offset = P10_OFFSET;
		p01_offset = P01_OFFSET;
	}
	a = interpolate(
		*(texel + p00_offset + A_OFFSET),
		*(texel + p01_offset + A_OFFSET),
		*(texel + p10_offset + A_OFFSET),
		ds,
		dt
	);
	r = interpolate(
		*(texel + p00_offset + R_OFFSET),
		*(texel + p01_offset + R_OFFSET),
		*(texel + p10_offset + R_OFFSET),
		ds,
		dt
	);
	g = interpolate(
		*(texel + p00_offset + G_OFFSET),
		*(texel + p01_offset + G_OFFSET),
		*(texel + p10_offset + G_OFFSET),
		ds,
		dt
	);
	b = interpolate(
		*(texel + p00_offset + B_OFFSET),
		*(texel + p01_offset + B_OFFSET),
		*(texel + p10_offset + B_OFFSET),
		ds,
		dt
	);
}

TexelBuffer *createBilinearTexelBuffer(byte *buf, const Graphics::PixelFormat &pf, uint format, uint type, uint width, uint height, uint textureSize) {
	return new BilinearTexelBuffer(
		buf, pf,
		width, height,
		textureSize
	);
}

} // end of namespace TinyGL