oes-texture-float-and-half-float-linear.js (6992B)
1 /* 2 Copyright (c) 2019 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 7 function testTexLinear(gl, extensionName, internalFormatWebgl2, pixelType) { 8 var wtu = WebGLTestUtils; 9 10 // Before the extension is enabled 11 var extensionEnabled = false; 12 runTestSuite(extensionEnabled); 13 14 if (!gl.getExtension(extensionName)) 15 testPassed("No " + extensionName + " support -- this is legal"); 16 else { 17 // After the extension is enabled 18 extensionEnabled = true; 19 runTestSuite(extensionEnabled); 20 } 21 22 function runTestSuite(extensionEnabled) 23 { 24 var magF = [gl.NEAREST, gl.LINEAR]; 25 var minF = [gl.NEAREST, gl.LINEAR, gl.NEAREST_MIPMAP_NEAREST, gl.NEAREST_MIPMAP_LINEAR, gl.LINEAR_MIPMAP_NEAREST, gl.LINEAR_MIPMAP_LINEAR]; 26 var tex2DFShader = [ 27 'uniform sampler2D tex;', 28 'void main() {', 29 ' gl_FragData[0] = texture2D(tex, vec2(0.5, 0.5)) * vec4(4.0, 2.0, 2.0, 1);', 30 '}'].join('\n'); 31 32 var positionVertexShader = [ 33 'attribute vec4 vPosition;', 34 'void main() {', 35 ' gl_Position = vPosition;', 36 '}'].join('\n'); 37 38 var texCubeFShader = [ 39 'uniform samplerCube tex;', 40 'void main() {', 41 ' gl_FragColor = textureCube(tex, normalize(vec3(0.5, 0.5, 1))) * vec4(4.0, 2.0, 2.0, 1);', 42 '}'].join('\n'); 43 44 var vs = wtu.loadShader(gl, positionVertexShader, gl.VERTEX_SHADER); 45 var fs_2d = wtu.loadShader(gl, tex2DFShader, gl.FRAGMENT_SHADER); 46 var fs_cube = wtu.loadShader(gl, texCubeFShader, gl.FRAGMENT_SHADER); 47 48 // TEXTURE_2D 49 var program = wtu.setupProgram(gl, [vs, fs_2d]); 50 gl.useProgram(program); 51 wtu.setupUnitQuad(gl); 52 for (var kk = 0; kk < 2; ++kk) { 53 for (var ii = 0; ii < 6; ++ii) { 54 var linear = false; 55 if (magF[kk] == gl.LINEAR || (minF[ii] != gl.NEAREST && minF[ii] != gl.NEAREST_MIPMAP_NEAREST)) 56 linear = true; 57 var color = (!extensionEnabled && linear) ? [0, 0, 0, 255] : [255, 255, 255, 255]; 58 runEachTest(gl.TEXTURE_2D, magF[kk], minF[ii], linear, extensionEnabled, color); 59 } 60 } 61 62 // TEXTURE_CUBE_MAP 63 var programCube = wtu.setupProgram(gl, [vs, fs_cube]); 64 gl.useProgram(programCube); 65 wtu.setupUnitQuad(gl); 66 for (var kk = 0; kk < 2; ++kk) { 67 for (var ii = 0; ii < 6; ++ii) { 68 var linear = false; 69 if (magF[kk] == gl.LINEAR || (minF[ii] != gl.NEAREST && minF[ii] != gl.NEAREST_MIPMAP_NEAREST)) 70 linear = true; 71 var color = (!extensionEnabled && linear) ? [0, 0, 0, 255] : [255, 255, 255, 255]; 72 runEachTest(gl.TEXTURE_CUBE_MAP, magF[kk], minF[ii], linear, extensionEnabled, color); 73 } 74 } 75 } 76 77 function runEachTest(textureTarget, magFilter, minFilter, linear, extensionEnabled, expected) 78 { 79 const format = gl.RGBA; 80 let internalFormat = format; 81 if (wtu.isWebGL2(gl)) { 82 internalFormat = gl[internalFormatWebgl2]; 83 } 84 var numberOfChannels = 4; 85 debug(""); 86 debug("testing target: " + wtu.glEnumToString(gl,textureTarget) + 87 ", testing format: " + wtu.glEnumToString(gl, format) + 88 ", magFilter is: " + wtu.glEnumToString(gl, magFilter) + 89 ", minFilter is: " + wtu.glEnumToString(gl, minFilter) + 90 ", " + extensionName + " is " + (extensionEnabled ? "enabled": "not enabled") 91 ); 92 93 // Generate data. 94 var width = 4; 95 var height = 4; 96 var canvas2d = document.createElement('canvas'); 97 canvas2d.width = width; 98 canvas2d.height = height; 99 var ctx2d = canvas2d.getContext('2d'); 100 var color = [64, 128, 128, 255]; 101 ctx2d.fillStyle = "rgba(" + color[0] + "," + color[1] + "," + color[2] + "," + color[3] + ")"; 102 ctx2d.fillRect(0, 0, width, height); 103 104 var texture = gl.createTexture(); 105 gl.bindTexture(textureTarget, texture); 106 gl.texParameteri(textureTarget, gl.TEXTURE_MAG_FILTER, magFilter); 107 gl.texParameteri(textureTarget, gl.TEXTURE_MIN_FILTER, minFilter); 108 gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); 109 gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); 110 111 if (textureTarget == gl.TEXTURE_2D) { 112 gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, format, gl[pixelType], canvas2d); 113 if (minFilter != gl.NEAREST && minFilter != gl.LINEAR) { 114 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors during texture setup"); 115 gl.generateMipmap(gl.TEXTURE_2D); 116 if (gl.getError() != gl.NO_ERROR) { 117 debug("generateMipmap failed for floating-point TEXTURE_2D -- this is legal -- skipping the rest of this test"); 118 return; 119 } 120 } 121 } else if (textureTarget == gl.TEXTURE_CUBE_MAP) { 122 var targets = [ 123 gl.TEXTURE_CUBE_MAP_POSITIVE_X, 124 gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 125 gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 126 gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 127 gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 128 gl.TEXTURE_CUBE_MAP_NEGATIVE_Z]; 129 for (var tt = 0; tt < targets.length; ++tt) 130 gl.texImage2D(targets[tt], 0, internalFormat, format, gl[pixelType], canvas2d); 131 if (minFilter != gl.NEAREST && minFilter != gl.LINEAR) { 132 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors during texture setup"); 133 gl.generateMipmap(gl.TEXTURE_CUBE_MAP); 134 if (gl.getError() != gl.NO_ERROR) { 135 debug("generateMipmap failed for floating-point TEXTURE_CUBE_MAP -- this is legal -- skipping the rest of this test"); 136 return; 137 } 138 } 139 } 140 wtu.clearAndDrawUnitQuad(gl); 141 if (!linear) { 142 wtu.glErrorShouldBe(gl, gl.NO_ERROR, pixelType + " texture with non-Linear filter should succeed with NO_ERROR no matter whether " + extensionName + " is enabled or not"); 143 } else if (!extensionEnabled) { 144 wtu.glErrorShouldBe(gl, gl.NO_ERROR, pixelType + " texture with Linear filter should produce [0, 0, 0, 1.0] with NO_ERROR if " + extensionName + " isn't enabled"); 145 } else { 146 wtu.glErrorShouldBe(gl, gl.NO_ERROR, pixelType + " texture with Linear filter should succeed with NO_ERROR if " + extensionName + " is enabled"); 147 } 148 149 wtu.checkCanvas(gl, expected, "should be " + expected[0] + "," + expected[1] + "," + expected[2] + "," + expected[3]); 150 } 151 }