|
| 1 | +<!-- |
| 2 | +Copyright (c) 2025 The Khronos Group Inc. |
| 3 | +Use of this source code is governed by an MIT-style license that can be |
| 4 | +found in the LICENSE.txt file. |
| 5 | +--> |
| 6 | +<!DOCTYPE html> |
| 7 | +<meta charset="utf-8"> |
| 8 | +<title>WebGL texImage2D w/ <img> with SVG image without natural width and height</title> |
| 9 | +<link rel="stylesheet" href="../../../resources/js-test-style.css"/> |
| 10 | +<script src="../../../js/js-test-pre.js"></script> |
| 11 | +<script src="../../../js/webgl-test-utils.js"> </script> |
| 12 | +<div id="description"></div> |
| 13 | +<div id="console"></div> |
| 14 | +<script> |
| 15 | +"use strict"; |
| 16 | + |
| 17 | +description("Test texImage2D from an img element with an SVG image without natural width and height."); |
| 18 | + |
| 19 | +const wtu = WebGLTestUtils; |
| 20 | + |
| 21 | +function makeTexture(image, variant) { |
| 22 | + const tex = gl.createTexture(); |
| 23 | + gl.bindTexture(gl.TEXTURE_2D, tex); |
| 24 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| 25 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| 26 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 27 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 28 | + |
| 29 | + if (variant === "texSubImage2D") { |
| 30 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, image.width, image.height, 0, gl.RGBA, |
| 31 | + gl.UNSIGNED_BYTE, null); |
| 32 | + gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, image); |
| 33 | + } else { |
| 34 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function testWidthHeightSet(image, variant, description) { |
| 39 | + debug(''); |
| 40 | + debug(`${variant} (HTMLImageElement.width/height set)`); |
| 41 | + |
| 42 | + makeTexture(image, variant); |
| 43 | + wtu.checkTextureSize(gl, 100, 100); |
| 44 | + |
| 45 | + wtu.clearAndDrawUnitQuad(gl); |
| 46 | + |
| 47 | + wtu.checkCanvasRect(gl, 75, 37, 2, 2, [0, 0, 255, 255], "should be blue"); |
| 48 | + wtu.checkCanvasRect(gl, 75, 108, 2, 2, [0, 0, 0, 0], "should be transparent"); |
| 49 | + wtu.checkCanvasRect(gl, 225, 108, 2, 2, [255, 255, 0, 255], "should be yellow"); |
| 50 | + wtu.checkCanvasRect(gl, 225, 37, 2, 2, [0, 0, 0, 0], "should be transparent"); |
| 51 | +} |
| 52 | + |
| 53 | +function testWidthHeightNotSet(image, variant) { |
| 54 | + debug(''); |
| 55 | + debug(`${variant} (HTMLImageElement.width/height not set)`); |
| 56 | + |
| 57 | + makeTexture(image, variant); |
| 58 | + while (gl.getError()) {} |
| 59 | + |
| 60 | + // Try to change the texture. This should fail because dimensions should have |
| 61 | + // been specified as 0x0. |
| 62 | + const buf = new Uint8Array(4); |
| 63 | + gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
| 64 | + wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "texture size should be 0x0"); |
| 65 | +} |
| 66 | + |
| 67 | +const SVG_IMAGE = `<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'> |
| 68 | + <rect width='50' height='50' fill='blue'/> |
| 69 | + <rect x='50' y='50' width='50' height='50' fill='yellow'/> |
| 70 | + </svg>`; |
| 71 | +const gl = wtu.create3DContext(); |
| 72 | +const program = wtu.setupTexturedQuad(gl); |
| 73 | + |
| 74 | +wtu.loadImageAsync(`data:image/svg+xml,${SVG_IMAGE}`, image => { |
| 75 | + testWidthHeightNotSet(image, "texImage2D"); |
| 76 | + testWidthHeightNotSet(image, "texSubImage2D"); |
| 77 | + |
| 78 | + image.width = 100; |
| 79 | + image.height = 100; |
| 80 | + |
| 81 | + testWidthHeightSet(image, "texImage2D"); |
| 82 | + testWidthHeightSet(image, "texSubImage2D"); |
| 83 | + |
| 84 | + debug(''); |
| 85 | + finishTest(); |
| 86 | +}); |
| 87 | + |
| 88 | +var successfullyParsed = true; |
| 89 | +</script> |
0 commit comments