From 5d39cf96ca459e8e2b6d91d1b3cece47125d10ff Mon Sep 17 00:00:00 2001 From: Martin Valigursky Date: Wed, 27 Sep 2023 11:57:41 +0100 Subject: [PATCH 1/2] Validate and enforce valid texture dimensions --- src/core/math/math.js | 11 +++++++++++ src/platform/graphics/texture.js | 11 +++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/core/math/math.js b/src/core/math/math.js index 0319d6c3962..a92baef61c0 100644 --- a/src/core/math/math.js +++ b/src/core/math/math.js @@ -18,6 +18,17 @@ const math = { */ RAD_TO_DEG: 180 / Math.PI, + /** + * Returns true if the specified value is a valid whole number. + * + * @param {object} value - The value to test. + * @returns {boolean} True if the value is a valid whole number. + * @ignore + */ + isInt: function (value) { + return Number.isFinite(value) && Math.floor(value) === value; + }, + /** * Clamp a number between min and max inclusive. * diff --git a/src/platform/graphics/texture.js b/src/platform/graphics/texture.js index 2e483a55e41..82ee23463c5 100644 --- a/src/platform/graphics/texture.js +++ b/src/platform/graphics/texture.js @@ -181,18 +181,21 @@ class Texture { constructor(graphicsDevice, options = {}) { this.device = graphicsDevice; Debug.assert(this.device, "Texture constructor requires a graphicsDevice to be valid"); + Debug.assert(!options.width || math.isInt(options.width), "Texture width must be an integer number, got", options); + Debug.assert(!options.height || math.isInt(options.height), "Texture height must be an integer number, got", options); + Debug.assert(!options.depth || math.isInt(options.depth), "Texture depth must be an integer number, got", options); - this.name = options.name ?? null; + this.name = options.name ?? ''; - this._width = options.width ?? 4; - this._height = options.height ?? 4; + this._width = Math.floor(options.width ?? 4); + this._height = Math.floor(options.height ?? 4); this._format = options.format ?? PIXELFORMAT_RGBA8; this._compressed = isCompressedPixelFormat(this._format); if (graphicsDevice.supportsVolumeTextures) { this._volume = options.volume ?? false; - this._depth = options.depth ?? 1; + this._depth = Math.floor(options.depth ?? 1); } else { this._volume = false; this._depth = 1; From 4327332b18a87806ef77cde1c90f575c7f8ac027 Mon Sep 17 00:00:00 2001 From: Martin Valigursky Date: Wed, 27 Sep 2023 12:40:26 +0100 Subject: [PATCH 2/2] update --- src/core/math/math.js | 11 ----------- src/platform/graphics/texture.js | 6 +++--- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/core/math/math.js b/src/core/math/math.js index a92baef61c0..0319d6c3962 100644 --- a/src/core/math/math.js +++ b/src/core/math/math.js @@ -18,17 +18,6 @@ const math = { */ RAD_TO_DEG: 180 / Math.PI, - /** - * Returns true if the specified value is a valid whole number. - * - * @param {object} value - The value to test. - * @returns {boolean} True if the value is a valid whole number. - * @ignore - */ - isInt: function (value) { - return Number.isFinite(value) && Math.floor(value) === value; - }, - /** * Clamp a number between min and max inclusive. * diff --git a/src/platform/graphics/texture.js b/src/platform/graphics/texture.js index 82ee23463c5..3bdf6e4ba2d 100644 --- a/src/platform/graphics/texture.js +++ b/src/platform/graphics/texture.js @@ -181,9 +181,9 @@ class Texture { constructor(graphicsDevice, options = {}) { this.device = graphicsDevice; Debug.assert(this.device, "Texture constructor requires a graphicsDevice to be valid"); - Debug.assert(!options.width || math.isInt(options.width), "Texture width must be an integer number, got", options); - Debug.assert(!options.height || math.isInt(options.height), "Texture height must be an integer number, got", options); - Debug.assert(!options.depth || math.isInt(options.depth), "Texture depth must be an integer number, got", options); + Debug.assert(!options.width || Number.isInteger(options.width), "Texture width must be an integer number, got", options); + Debug.assert(!options.height || Number.isInteger(options.height), "Texture height must be an integer number, got", options); + Debug.assert(!options.depth || Number.isInteger(options.depth), "Texture depth must be an integer number, got", options); this.name = options.name ?? '';