[Scummvm-git-logs] scummvm master -> 320492d4428923a7f73d8191c3461138308652a8

ccawley2011 noreply at scummvm.org
Fri Feb 3 18:06:56 UTC 2023


This automated email contains information about 3 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
4c1fdb81d5 TINYGL: Make use of ColorMask templates for NearestTexelBuffer
2773eea09d TINYGL: Move more TexelBuffer code into texelbuffer.cpp
320492d442 TINYGL: Use gl_malloc() and gl_free() for the TexelBuffer classes


Commit: 4c1fdb81d56956784694a53e39835eb69d4d84ba
    https://github.com/scummvm/scummvm/commit/4c1fdb81d56956784694a53e39835eb69d4d84ba
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2023-02-03T18:06:51Z

Commit Message:
TINYGL: Make use of ColorMask templates for NearestTexelBuffer

Changed paths:
  A graphics/tinygl/colormasks.h
    graphics/tinygl/texelbuffer.cpp
    graphics/tinygl/texelbuffer.h
    graphics/tinygl/texture.cpp


diff --git a/graphics/tinygl/colormasks.h b/graphics/tinygl/colormasks.h
new file mode 100644
index 00000000000..734ba30ddd2
--- /dev/null
+++ b/graphics/tinygl/colormasks.h
@@ -0,0 +1,146 @@
+/* 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/>.
+ *
+ */
+
+#ifndef GRAPHICS_TINYGL_COLORMASKS_H
+#define GRAPHICS_TINYGL_COLORMASKS_H
+
+#include "graphics/tinygl/gl.h"
+
+namespace TinyGL {
+
+template<uint Format, uint Type>
+struct ColorMasks {
+};
+
+template<>
+struct ColorMasks<TGL_RGB, TGL_UNSIGNED_SHORT_5_6_5> {
+	enum : uint {
+		kBytesPerPixel = 2,
+
+		kAlphaBits  = 0,
+		kRedBits    = 5,
+		kGreenBits  = 6,
+		kBlueBits   = 5,
+
+		kAlphaShift = 0,
+		kRedShift   = kGreenBits+kBlueBits,
+		kGreenShift = kBlueBits,
+		kBlueShift  = 0,
+
+		kAlphaMask  = ((1 << kAlphaBits) - 1) << kAlphaShift,
+		kRedMask    = ((1 << kRedBits) - 1) << kRedShift,
+		kGreenMask  = ((1 << kGreenBits) - 1) << kGreenShift,
+		kBlueMask   = ((1 << kBlueBits) - 1) << kBlueShift,
+
+		kRedBlueMask = kRedMask | kBlueMask,
+	};
+
+	typedef uint16 PixelType;
+};
+
+template<>
+struct ColorMasks<TGL_RGBA, TGL_UNSIGNED_SHORT_5_5_5_1> {
+	enum {
+		kBytesPerPixel = 2,
+
+		kAlphaBits  = 1,
+		kRedBits    = 5,
+		kGreenBits  = 5,
+		kBlueBits   = 5,
+
+		kAlphaShift = 0,
+		kRedShift   = kGreenBits+kBlueBits+kAlphaBits,
+		kGreenShift = kBlueBits+kAlphaBits,
+		kBlueShift  = kAlphaBits,
+
+		kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
+		kRedMask   = ((1 << kRedBits) - 1) << kRedShift,
+		kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
+		kBlueMask  = ((1 << kBlueBits) - 1) << kBlueShift,
+
+		kRedBlueMask = kRedMask | kBlueMask
+	};
+
+	typedef uint16 PixelType;
+};
+
+template<>
+struct ColorMasks<TGL_RGBA, TGL_UNSIGNED_SHORT_4_4_4_4> {
+	enum {
+		kBytesPerPixel = 2,
+
+		kAlphaBits  = 4,
+		kRedBits    = 4,
+		kGreenBits  = 4,
+		kBlueBits   = 4,
+
+		kAlphaShift = 0,
+		kRedShift   = kGreenBits+kBlueBits+kAlphaBits,
+		kGreenShift = kBlueBits+kAlphaBits,
+		kBlueShift  = kAlphaBits,
+
+		kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
+		kRedMask   = ((1 << kRedBits) - 1) << kRedShift,
+		kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
+		kBlueMask  = ((1 << kBlueBits) - 1) << kBlueShift,
+
+		kRedBlueMask = kRedMask | kBlueMask
+	};
+
+	typedef uint16 PixelType;
+};
+
+template<>
+struct ColorMasks<TGL_RGBA, TGL_UNSIGNED_BYTE> {
+	enum {
+		kBytesPerPixel = 4,
+
+		kAlphaBits  = 8,
+		kRedBits    = 8,
+		kGreenBits  = 8,
+		kBlueBits   = 8,
+
+#if defined(SCUMM_LITTLE_ENDIAN)
+		kAlphaShift = kRedBits+kGreenBits+kBlueBits,
+		kRedShift   = 0,
+		kGreenShift = kRedBits,
+		kBlueShift  = kRedBits+kGreenBits,
+#else
+		kAlphaShift = 0,
+		kRedShift   = kGreenBits+kBlueBits+kAlphaBits,
+		kGreenShift = kBlueBits+kAlphaBits,
+		kBlueShift  = kAlphaBits,
+#endif
+
+		kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
+		kRedMask   = ((1 << kRedBits) - 1) << kRedShift,
+		kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
+		kBlueMask  = ((1 << kBlueBits) - 1) << kBlueShift,
+
+		kRedBlueMask = kRedMask | kBlueMask,
+	};
+
+	typedef uint32 PixelType;
+};
+
+} // End of namespace Graphics
+
+#endif
diff --git a/graphics/tinygl/texelbuffer.cpp b/graphics/tinygl/texelbuffer.cpp
index 673a3353b7e..318ccaaccad 100644
--- a/graphics/tinygl/texelbuffer.cpp
+++ b/graphics/tinygl/texelbuffer.cpp
@@ -22,6 +22,7 @@
 #include "graphics/tinygl/gl.h"
 #include "graphics/tinygl/zgl.h"
 #include "graphics/tinygl/zbuffer.h"
+#include "graphics/tinygl/pixelbuffer.h"
 #include "graphics/tinygl/texelbuffer.h"
 
 namespace TinyGL {
@@ -77,22 +78,14 @@ void TexelBuffer::getARGBAt(
 }
 
 // Nearest: store texture in original size.
-NearestTexelBuffer::NearestTexelBuffer(const Graphics::PixelBuffer &buf, uint width, uint height, uint textureSize) : TexelBuffer(width, height, textureSize) {
-	uint pixel_count = _width * _height;
-	_buf = Graphics::PixelBuffer(buf.getFormat(), pixel_count, DisposeAfterUse::NO);
-	_buf.copyBuffer(0, pixel_count, buf);
+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 = new byte[count];
+	memcpy(_buf, buf, count);
 }
 
-NearestTexelBuffer::~NearestTexelBuffer() {
-	_buf.free();
-}
-
-void NearestTexelBuffer::getARGBAt(
-	uint pixel,
-	uint, uint,
-	uint8 &a, uint8 &r, uint8 &g, uint8 &b
-) const {
-	_buf.getARGBAt(pixel, a, r, g, b);
+BaseNearestTexelBuffer::~BaseNearestTexelBuffer() {
+	delete[] _buf;
 }
 
 // Bilinear: each texture coordinates corresponds to the 4 original image
@@ -111,7 +104,9 @@ void NearestTexelBuffer::getARGBAt(
 #define P11_OFFSET 3
 #define PIXEL_PER_TEXEL_SHIFT 2
 
-BilinearTexelBuffer::BilinearTexelBuffer(const Graphics::PixelBuffer &buf, uint width, uint height, uint textureSize) : TexelBuffer(width, height, textureSize) {
+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;
@@ -121,7 +116,7 @@ BilinearTexelBuffer::BilinearTexelBuffer(const Graphics::PixelBuffer &buf, uint
 		for (uint x = 0; x < _width; x++) {
 			texel8 = (uint8 *)texel32;
 			pixel11_offset = pixel00_offset + _width + 1;
-			buf.getARGBAt(
+			src.getARGBAt(
 				pixel00_offset,
 				*(texel8 + P00_OFFSET + A_OFFSET),
 				*(texel8 + P00_OFFSET + R_OFFSET),
@@ -133,7 +128,7 @@ BilinearTexelBuffer::BilinearTexelBuffer(const Graphics::PixelBuffer &buf, uint
 				pixel01_offset = pixel00_offset;
 			} else
 				pixel01_offset = pixel00_offset + 1;
-			buf.getARGBAt(
+			src.getARGBAt(
 				pixel01_offset,
 				*(texel8 + P01_OFFSET + A_OFFSET),
 				*(texel8 + P01_OFFSET + R_OFFSET),
@@ -145,14 +140,14 @@ BilinearTexelBuffer::BilinearTexelBuffer(const Graphics::PixelBuffer &buf, uint
 				pixel10_offset = pixel00_offset;
 			} else
 				pixel10_offset = pixel00_offset + _width;
-			buf.getARGBAt(
+			src.getARGBAt(
 				pixel10_offset,
 				*(texel8 + P10_OFFSET + A_OFFSET),
 				*(texel8 + P10_OFFSET + R_OFFSET),
 				*(texel8 + P10_OFFSET + G_OFFSET),
 				*(texel8 + P10_OFFSET + B_OFFSET)
 			);
-			buf.getARGBAt(
+			src.getARGBAt(
 				pixel11_offset,
 				*(texel8 + P11_OFFSET + A_OFFSET),
 				*(texel8 + P11_OFFSET + R_OFFSET),
diff --git a/graphics/tinygl/texelbuffer.h b/graphics/tinygl/texelbuffer.h
index d0aabf20e99..f096345a554 100644
--- a/graphics/tinygl/texelbuffer.h
+++ b/graphics/tinygl/texelbuffer.h
@@ -22,7 +22,8 @@
 #ifndef GRAPHICS_TEXELBUFFER_H
 #define GRAPHICS_TEXELBUFFER_H
 
-#include "graphics/tinygl/pixelbuffer.h"
+#include "graphics/pixelformat.h"
+#include "graphics/tinygl/colormasks.h"
 
 namespace TinyGL {
 
@@ -47,25 +48,59 @@ protected:
 	float _widthRatio, _heightRatio;
 };
 
-class NearestTexelBuffer : public TexelBuffer {
+class BaseNearestTexelBuffer : public TexelBuffer {
 public:
-	NearestTexelBuffer(const Graphics::PixelBuffer &buf, uint width, uint height, uint textureSize);
-	~NearestTexelBuffer();
+	BaseNearestTexelBuffer(const byte *buf, const Graphics::PixelFormat &format, uint width, uint height, uint textureSize);
+	~BaseNearestTexelBuffer();
+
+protected:
+	byte *_buf;
+	Graphics::PixelFormat _format;
+};
+
+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;
+	) const override {
+		Pixel col = *(((const Pixel *)_buf) + pixel);
+		_format.colorToARGBT<ColorMask>(col, a, r, g, b);
+	}
 
-private:
-	Graphics::PixelBuffer _buf;
+	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];
+	}
 };
 
 class BilinearTexelBuffer : public TexelBuffer {
 public:
-	BilinearTexelBuffer(const Graphics::PixelBuffer &buf, uint width, uint height, uint textureSize);
+	BilinearTexelBuffer(byte *buf, const Graphics::PixelFormat &format, uint width, uint height, uint textureSize);
 	~BilinearTexelBuffer();
 
 protected:
diff --git a/graphics/tinygl/texture.cpp b/graphics/tinygl/texture.cpp
index aec1fe348d2..b1f45fe4502 100644
--- a/graphics/tinygl/texture.cpp
+++ b/graphics/tinygl/texture.cpp
@@ -155,21 +155,7 @@ void GLContext::glopTexImage2D(GLParam *p) {
 		}
 		if (!found)
 			error("TinyGL texture: format 0x%04x and type 0x%04x combination not supported", format, type);
-		Graphics::PixelBuffer src(pf, pixels);
-		Graphics::PixelFormat internalPf;
-#if defined(SCUMM_LITTLE_ENDIAN)
-		if (internalformat == TGL_RGBA)
-			internalPf = Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
-		else if (internalformat == TGL_RGB)
-			internalPf = Graphics::PixelFormat(3, 8, 8, 8, 0, 0, 8, 16, 0);
-#else
-		if (internalformat == TGL_RGBA)
-			internalPf = Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
-		else if (internalformat == TGL_RGB)
-			internalPf = Graphics::PixelFormat(3, 8, 8, 8, 0, 16, 8, 0, 0);
-#endif
-		Graphics::PixelBuffer srcInternal(internalPf, width * height, DisposeAfterUse::YES);
-		srcInternal.copyBuffer(0, width * height, src);
+
 		if (width > _textureSize || height > _textureSize)
 			filter = texture_mag_filter;
 		else
@@ -179,17 +165,45 @@ void GLContext::glopTexImage2D(GLParam *p) {
 		case TGL_LINEAR_MIPMAP_LINEAR:
 		case TGL_LINEAR:
 			im->pixmap = new BilinearTexelBuffer(
-				srcInternal,
+				pixels, pf,
 				width, height,
 				_textureSize
 			);
 			break;
 		default:
-			im->pixmap = new NearestTexelBuffer(
-				srcInternal,
-				width, height,
-				_textureSize
-			);
+			if (format == TGL_RGBA && type == TGL_UNSIGNED_BYTE) {
+				im->pixmap = new NearestTexelBuffer<TGL_RGBA, TGL_UNSIGNED_BYTE>(
+					pixels, pf,
+					width, height,
+					_textureSize
+				);
+			} else if (format == TGL_RGB && type == TGL_UNSIGNED_BYTE) {
+				im->pixmap = new NearestTexelBuffer<TGL_RGB,  TGL_UNSIGNED_BYTE>(
+					pixels, pf,
+					width, height,
+					_textureSize
+				);
+			} else if (format == TGL_RGB && type == TGL_UNSIGNED_SHORT_5_6_5) {
+				im->pixmap = new NearestTexelBuffer<TGL_RGB,  TGL_UNSIGNED_SHORT_5_6_5>(
+					pixels, pf,
+					width, height,
+					_textureSize
+				);
+			} else if (format == TGL_RGBA && type == TGL_UNSIGNED_SHORT_5_5_5_1) {
+				im->pixmap = new NearestTexelBuffer<TGL_RGBA, TGL_UNSIGNED_SHORT_5_5_5_1>(
+					pixels, pf,
+					width, height,
+					_textureSize
+				);
+			} else if (format == TGL_RGBA && type == TGL_UNSIGNED_SHORT_4_4_4_4) {
+				im->pixmap = new NearestTexelBuffer<TGL_RGBA, TGL_UNSIGNED_SHORT_4_4_4_4>(
+					pixels, pf,
+					width, height,
+					_textureSize
+				);
+			} else {
+				error("TinyGL texture: format 0x%04x and type 0x%04x combination not supported", format, type);
+			}
 			break;
 		}
 	}


Commit: 2773eea09d5e992e5ffc0fb54ec90d585a0664b1
    https://github.com/scummvm/scummvm/commit/2773eea09d5e992e5ffc0fb54ec90d585a0664b1
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2023-02-03T18:06:51Z

Commit Message:
TINYGL: Move more TexelBuffer code into texelbuffer.cpp

Changed paths:
    graphics/tinygl/texelbuffer.cpp
    graphics/tinygl/texelbuffer.h
    graphics/tinygl/texture.cpp


diff --git a/graphics/tinygl/texelbuffer.cpp b/graphics/tinygl/texelbuffer.cpp
index 318ccaaccad..aeee2874e20 100644
--- a/graphics/tinygl/texelbuffer.cpp
+++ b/graphics/tinygl/texelbuffer.cpp
@@ -22,6 +22,7 @@
 #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"
 
@@ -78,6 +79,16 @@ void TexelBuffer::getARGBAt(
 }
 
 // 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 = new byte[count];
@@ -88,12 +99,104 @@ BaseNearestTexelBuffer::~BaseNearestTexelBuffer() {
 	delete[] _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)
@@ -216,4 +319,12 @@ void BilinearTexelBuffer::getARGBAt(
 	);
 }
 
+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
diff --git a/graphics/tinygl/texelbuffer.h b/graphics/tinygl/texelbuffer.h
index f096345a554..596e95c40a2 100644
--- a/graphics/tinygl/texelbuffer.h
+++ b/graphics/tinygl/texelbuffer.h
@@ -23,7 +23,6 @@
 #define GRAPHICS_TEXELBUFFER_H
 
 #include "graphics/pixelformat.h"
-#include "graphics/tinygl/colormasks.h"
 
 namespace TinyGL {
 
@@ -48,71 +47,8 @@ protected:
 	float _widthRatio, _heightRatio;
 };
 
-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;
-};
-
-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];
-	}
-};
-
-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;
-};
+TexelBuffer *createNearestTexelBuffer(const byte *buf, const Graphics::PixelFormat &pf, uint format, uint type, uint width, uint height, uint textureSize);
+TexelBuffer *createBilinearTexelBuffer(byte *buf, const Graphics::PixelFormat &pf, uint format, uint type, uint width, uint height, uint textureSize);
 
 } // end of namespace TinyGL
 
diff --git a/graphics/tinygl/texture.cpp b/graphics/tinygl/texture.cpp
index b1f45fe4502..1b421648bf0 100644
--- a/graphics/tinygl/texture.cpp
+++ b/graphics/tinygl/texture.cpp
@@ -164,46 +164,20 @@ void GLContext::glopTexImage2D(GLParam *p) {
 		case TGL_LINEAR_MIPMAP_NEAREST:
 		case TGL_LINEAR_MIPMAP_LINEAR:
 		case TGL_LINEAR:
-			im->pixmap = new BilinearTexelBuffer(
+			im->pixmap = createBilinearTexelBuffer(
 				pixels, pf,
+				format, type,
 				width, height,
 				_textureSize
 			);
 			break;
 		default:
-			if (format == TGL_RGBA && type == TGL_UNSIGNED_BYTE) {
-				im->pixmap = new NearestTexelBuffer<TGL_RGBA, TGL_UNSIGNED_BYTE>(
-					pixels, pf,
-					width, height,
-					_textureSize
-				);
-			} else if (format == TGL_RGB && type == TGL_UNSIGNED_BYTE) {
-				im->pixmap = new NearestTexelBuffer<TGL_RGB,  TGL_UNSIGNED_BYTE>(
-					pixels, pf,
-					width, height,
-					_textureSize
-				);
-			} else if (format == TGL_RGB && type == TGL_UNSIGNED_SHORT_5_6_5) {
-				im->pixmap = new NearestTexelBuffer<TGL_RGB,  TGL_UNSIGNED_SHORT_5_6_5>(
-					pixels, pf,
-					width, height,
-					_textureSize
-				);
-			} else if (format == TGL_RGBA && type == TGL_UNSIGNED_SHORT_5_5_5_1) {
-				im->pixmap = new NearestTexelBuffer<TGL_RGBA, TGL_UNSIGNED_SHORT_5_5_5_1>(
-					pixels, pf,
-					width, height,
-					_textureSize
-				);
-			} else if (format == TGL_RGBA && type == TGL_UNSIGNED_SHORT_4_4_4_4) {
-				im->pixmap = new NearestTexelBuffer<TGL_RGBA, TGL_UNSIGNED_SHORT_4_4_4_4>(
-					pixels, pf,
-					width, height,
-					_textureSize
-				);
-			} else {
-				error("TinyGL texture: format 0x%04x and type 0x%04x combination not supported", format, type);
-			}
+			im->pixmap = createNearestTexelBuffer(
+				pixels, pf,
+				format, type,
+				width, height,
+				_textureSize
+			);
 			break;
 		}
 	}


Commit: 320492d4428923a7f73d8191c3461138308652a8
    https://github.com/scummvm/scummvm/commit/320492d4428923a7f73d8191c3461138308652a8
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2023-02-03T18:06:51Z

Commit Message:
TINYGL: Use gl_malloc() and gl_free() for the TexelBuffer classes

Changed paths:
    graphics/tinygl/texelbuffer.cpp


diff --git a/graphics/tinygl/texelbuffer.cpp b/graphics/tinygl/texelbuffer.cpp
index aeee2874e20..f9ab7daa5c0 100644
--- a/graphics/tinygl/texelbuffer.cpp
+++ b/graphics/tinygl/texelbuffer.cpp
@@ -91,12 +91,12 @@ protected:
 
 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 = new byte[count];
+	_buf = (byte *)gl_malloc(count);
 	memcpy(_buf, buf, count);
 }
 
 BaseNearestTexelBuffer::~BaseNearestTexelBuffer() {
-	delete[] _buf;
+	gl_free(_buf);
 }
 
 template<uint Format, uint Type>
@@ -214,7 +214,7 @@ BilinearTexelBuffer::BilinearTexelBuffer(byte *buf, const Graphics::PixelFormat
 	uint8 *texel8;
 	uint32 *texel32;
 
-	texel32 = _texels = new uint32[_width * _height << PIXEL_PER_TEXEL_SHIFT];
+	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;
@@ -264,7 +264,7 @@ BilinearTexelBuffer::BilinearTexelBuffer(byte *buf, const Graphics::PixelFormat
 }
 
 BilinearTexelBuffer::~BilinearTexelBuffer() {
-	delete[] _texels;
+	gl_free(_texels);
 }
 
 static inline int interpolate(int v00, int v01, int v10, int xf, int yf) {




More information about the Scummvm-git-logs mailing list