Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test uploading individual LOD levels to 3D textures from images #3699

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 126 additions & 3 deletions sdk/tests/js/tests/tex-image-and-sub-image-3d-with-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ function generateTest(internalFormat, pixelFormat, pixelType, prologue, resource
var blueColor = [0, 0, 255];
var cyanColor = [0, 255, 255];
var imageURLs = [resourcePath + "red-green.png",
resourcePath + "red-green-blue-cyan-4x4.png"];
resourcePath + "red-green-blue-cyan-4x4.png",
resourcePath + "red-green-blue-cyan-8x8x4-mipmaps.png",
resourcePath + "red-green-blue-cyan-128x128x4-mipmaps.png"];

function init()
{
Expand Down Expand Up @@ -55,7 +57,7 @@ function generateTest(internalFormat, pixelFormat, pixelType, prologue, resource
}

function uploadImageToTexture(image, useTexSubImage3D, flipY, bindingTarget,
depth, sourceSubRectangle, unpackImageHeight)
depth, sourceSubRectangle, unpackImageHeight, mipmaps)
{
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Disable any writes to the alpha channel
Expand All @@ -64,7 +66,7 @@ function generateTest(internalFormat, pixelFormat, pixelType, prologue, resource
// Bind the texture to texture unit 0
gl.bindTexture(bindingTarget, texture);
// Set up texture parameters
gl.texParameteri(bindingTarget, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(bindingTarget, gl.TEXTURE_MIN_FILTER, mipmaps ? gl.NEAREST_MIPMAP_NEAREST : gl.NEAREST);
gl.texParameteri(bindingTarget, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(bindingTarget, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(bindingTarget, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
Expand Down Expand Up @@ -94,6 +96,26 @@ function generateTest(internalFormat, pixelFormat, pixelType, prologue, resource
} else {
gl.texImage3D(bindingTarget, 0, gl[internalFormat], uploadWidth, uploadHeight, depth, 0,
gl[pixelFormat], gl[pixelType], image);
if (mipmaps) {
// Upload mipmaps packed beneath the specified image
var sourceY = sourceSubRectangle ? sourceSubRectangle[1] : 0;
var lastWidth = uploadWidth;
var lastHeight = uploadHeight;
var lastDepth = depth;
var level = 0;
while (lastWidth > 1 || lastHeight > 1) {
sourceY += lastHeight * depth;
++level;
lastWidth = Math.max(1, Math.floor(lastWidth/2));
lastHeight = Math.max(1, Math.floor(lastHeight/2));
if (bindingTarget == gl.TEXTURE_3D) {
lastDepth = Math.max(1, Math.floor(lastDepth/2));
}
gl.pixelStorei(gl.UNPACK_SKIP_ROWS, sourceY);
gl.texImage3D(bindingTarget, level, gl[internalFormat], lastWidth, lastHeight, lastDepth, 0,
gl[pixelFormat], gl[pixelType], image);
}
}
}
gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 0);
gl.pixelStorei(gl.UNPACK_SKIP_ROWS, 0);
Expand Down Expand Up @@ -248,10 +270,111 @@ function generateTest(internalFormat, pixelFormat, pixelType, prologue, resource
}
}

function runMipMapTest(image) {

var width = image.width;
var sourceSubRectangle = [0, 0, width, width];
var depth = 4;
var useTexSubImage3D = false;
var useMinMax = false;
var tolerance = 3; // Since using non-0/1 values, on various formats the results won't be exact

function runOneIteration(bindingTarget, rTextureCoord, lod, color, program)
{
debug('Testing ' + (useTexSubImage3D ? 'texSubImage3D' : 'texImage3D') +
' with bindingTarget=' +
(bindingTarget == gl.TEXTURE_3D ? 'TEXTURE_3D' : 'TEXTURE_2D_ARRAY') + ' with mipmaps from ' +
image.width + 'x' + image.height + ' with r=' + rTextureCoord + ', lod=' + lod);

uploadImageToTexture(image, useTexSubImage3D, false, bindingTarget, depth, sourceSubRectangle, undefined, true);

var rCoordLocation = gl.getUniformLocation(program, 'uRCoord');
if (!rCoordLocation) {
testFailed('Shader incorrectly set up; couldn\'t find uRCoord uniform');
return;
}
gl.uniform1f(rCoordLocation, rTextureCoord);

if (useMinMax) {
// Note: this seems like it should work, but fails on uint textures,
// bug in test / test author, or bug in implementations?
gl.texParameterf(bindingTarget, gl.TEXTURE_MIN_LOD, lod);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using TEXTURE_MIN/MAX_LOD here works in all tests except for the unsigned integer formats (where it appears to sample from the wrong level). I'm not sure if that means there's something I don't understand, or if I've stumbled upon a bug. I'd be happy to change this code to also test the alternative way to choose which lod levels are sampled from, or to remove this (currently unreachable) branch if desired.

gl.texParameterf(bindingTarget, gl.TEXTURE_MAX_LOD, lod);
} else {
gl.texParameteri(bindingTarget, gl.TEXTURE_BASE_LEVEL, lod);
gl.texParameteri(bindingTarget, gl.TEXTURE_MAX_LEVEL, lod);
}

// Draw the triangles
wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]);
// Check a few pixels near the top and bottom and make sure they have
// the right color.
var coord = Math.floor(gl.canvas.height/2);
wtu.checkCanvasRect(gl, coord, coord, 2, 2, color,
"shouldBe " + color, tolerance);

if (useMinMax) {
gl.texParameterf(bindingTarget, gl.TEXTURE_MIN_LOD, 0);
gl.texParameterf(bindingTarget, gl.TEXTURE_MAX_LOD, Infinity);
} else {
gl.texParameteri(bindingTarget, gl.TEXTURE_BASE_LEVEL, 0);
gl.texParameteri(bindingTarget, gl.TEXTURE_MAX_LEVEL, Infinity);
}
}

function masked(r, g, b) {
return [r, greenColor[1] ? g : 0, blueColor[2] ? b : 0];
}

var levels = internalFormat[0] === 'S' ? [
// SRGB values
0xFF, 0xB8, 0x7F
] : [
// Raw values from mipmaps in texture
0xFF, 0xDD, 0xBB
];
var cases = [
{ rTextureCoord: 0, lod: 0, color: masked(levels[0], 0, 0) },
{ rTextureCoord: 0.33, lod: 0, color: masked(0, levels[0], 0) },
{ rTextureCoord: 0.66, lod: 0, color: masked(0, 0, levels[0]) },
{ rTextureCoord: 1, lod: 0, color: masked(0, levels[0], levels[0]) },
{ rTextureCoord: 0, lod: 1, color: masked(levels[1], 0, 0) },
{ rTextureCoord: 1, lod: 1, color: masked(0, levels[1], 0) },
{ rTextureCoord: 0, lod: 2, color: masked(levels[2], 0, 0) },
];

var program = tiu.setupTexturedQuadWith3D(gl, internalFormat);
for (var i in cases) {
runOneIteration(gl.TEXTURE_3D, cases[i].rTextureCoord, cases[i].lod,
cases[i].color, program);
}
cases = [
{ rTextureCoord: 0, lod: 0, color: masked(levels[0], 0, 0) },
{ rTextureCoord: 1, lod: 0, color: masked(0, levels[0], 0) },
{ rTextureCoord: 2, lod: 0, color: masked(0, 0, levels[0]) },
{ rTextureCoord: 3, lod: 0, color: masked(0, levels[0], levels[0]) },
{ rTextureCoord: 0, lod: 1, color: masked(levels[1], 0, 0) },
{ rTextureCoord: 1, lod: 1, color: masked(0, levels[1], 0) },
{ rTextureCoord: 2, lod: 1, color: masked(0, 0, levels[1]) },
{ rTextureCoord: 3, lod: 1, color: masked(0, levels[1], levels[1]) },
{ rTextureCoord: 0, lod: 2, color: masked(levels[2], 0, 0) },
{ rTextureCoord: 1, lod: 2, color: masked(0, levels[2], 0) },
{ rTextureCoord: 2, lod: 2, color: masked(0, 0, levels[2]) },
{ rTextureCoord: 3, lod: 2, color: masked(0, levels[2], levels[2]) },
];
program = tiu.setupTexturedQuadWith2DArray(gl, internalFormat);
for (var i in cases) {
runOneIteration(gl.TEXTURE_2D_ARRAY, cases[i].rTextureCoord, cases[i].lod,
cases[i].color, program);
}
}

function runTest(imageMap)
{
runRedGreenTest(imageMap[imageURLs[0]]);
runRedGreenBlueCyanTest(imageMap[imageURLs[1]]);
runMipMapTest(imageMap[imageURLs[2]]);
runMipMapTest(imageMap[imageURLs[3]]);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
finishTest();
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.