1/* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import hilog from '@ohos.hilog'; 17import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, TestType, Level, Size } from '@ohos/hypium'; 18 19import {checkError, createProgram, getColorUint8Array} from './WebGL1'; 20 21 22export default function webgl1_texture() { 23 24 describe('webgl1_texture', function () { 25 let gl = global.gl; 26 // Defines a test suite. Two parameters are supported: test suite name and test suite function. 27 beforeAll(function () { 28 hilog.info(0x0000, 'testTag', '%{public}s', 'webgl1_test_01 start'); 29 // Presets an action, which is performed only once before all test cases of the test suite start. 30 // This API supports only one parameter: preset action function. 31 }) 32 beforeEach(function () { 33 // Presets an action, which is performed before each unit test case starts. 34 // The number of execution times is the same as the number of test cases defined by **it**. 35 // This API supports only one parameter: preset action function. 36 checkError(gl); 37 }) 38 afterEach(function () { 39 // Presets a clear action, which is performed after each unit test case ends. 40 // The number of execution times is the same as the number of test cases defined by **it**. 41 // This API supports only one parameter: clear action function. 42 checkError(gl); 43 }) 44 afterAll(function () { 45 // Presets a clear action, which is performed after all test cases of the test suite end. 46 // This API supports only one parameter: clear action function. 47 hilog.info(0x0000, 'testTag', '%{public}s', 'webgl1_test_01 end'); 48 }) 49 50 let initShader = (gl, type, source) => { 51 let shader = gl.createShader(type); // 创建顶点着色器 52 if (shader == null) { 53 console.info("webgltest ", 'unable to create shader'); 54 return null; 55 } 56 gl.shaderSource(shader, source); // 设置着色器代码 57 gl.compileShader(shader); 58 let shaderParameter = gl.getShaderParameter(shader, gl.COMPILE_STATUS); 59 if (!shaderParameter) { 60 let error = gl.getShaderInfoLog(shader); 61 console.info("webgltest ", 'failed to compile shader: ' + error); 62 gl.deleteShader(shader); 63 return null; 64 } 65 return shader; 66 } 67 68 /** 69 * 创建并初始化 WebGLTexture 对象 70 */ 71 72 /** 73 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0001 74 * @tc.name webgl_test_createTexture 75 * @tc.desc Test createTexture. 76 */ 77 it('webgl_test_createTexture', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 78 console.info("webgltest [webgl_test_createTexture] createTexture"); 79 let texture = gl.createTexture(); 80 console.info("webgltest ", texture); 81 expect(texture != null).assertTrue(); 82 console.info("webgltest ", "gl.bindTexture(gl.TEXTURE_2D, texture);"); 83 gl.bindTexture(gl.TEXTURE_2D, texture); 84 console.info("webgltest ", "isTexture :", gl.isTexture(texture)); 85 expect(gl.isTexture(texture)).assertTrue(); 86 console.info("webgltest ", "gl.deleteTexture(texture);"); 87 gl.deleteTexture(texture); 88 console.info("webgltest ", "isTexture :", gl.isTexture(texture)); 89 expect(gl.isTexture(texture) == false).assertTrue(); 90 checkError(gl); 91 done(); 92 }) 93 94 /** 95 * 如果传递的 WebGLTexture 有效,则 WebGL API 的方法返回 true,否则返回 false。 96 */ 97 98 /** 99 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0002 100 * @tc.name webgl_test_isTexture 101 * @tc.desc Test isTexture. 102 */ 103 it('webgl_test_isTexture', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 104 console.info("webgltest [webgl_test_isTexture] isTexture"); 105 let texture = gl.createTexture(); 106 console.info("webgltest ", texture); 107 expect(texture != null).assertTrue(); 108 console.info("webgltest ", "gl.bindTexture(gl.TEXTURE_2D, texture);"); 109 gl.bindTexture(gl.TEXTURE_2D, texture); 110 console.info("webgltest ", "isTexture :", gl.isTexture(texture)); 111 expect(gl.isTexture(texture)).assertTrue(); 112 console.info("webgltest ", "gl.deleteTexture(texture);"); 113 gl.deleteTexture(texture); 114 console.info("webgltest ", "isTexture :", gl.isTexture(texture)); 115 expect(gl.isTexture(texture) == false).assertTrue(); 116 checkError(gl); 117 done(); 118 }) 119 120 /** 121 * WebGL API 的方法删除给定的 WebGLTexture 对象。 如果纹理已被删除,则此方法不起作用。 122 */ 123 124 /** 125 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0003 126 * @tc.name webgl_test_deleteTexture 127 * @tc.desc Test deleteTexture. 128 */ 129 it('webgl_test_deleteTexture', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 130 console.info("webgltest [webgl_test_deleteTexture] deleteTexture"); 131 let texture = gl.createTexture(); 132 console.info("webgltest ", texture); 133 expect(texture != null).assertTrue(); 134 console.info("webgltest ", "gl.bindTexture(gl.TEXTURE_2D, texture);"); 135 gl.bindTexture(gl.TEXTURE_2D, texture); 136 console.info("webgltest ", "isTexture :", gl.isTexture(texture)); 137 expect(gl.isTexture(texture)).assertTrue(); 138 console.info("webgltest ", "gl.deleteTexture(texture);"); 139 gl.deleteTexture(texture); 140 console.info("webgltest ", "isTexture :", gl.isTexture(texture)); 141 expect(gl.isTexture(texture) == false).assertTrue(); 142 checkError(gl); 143 done(); 144 }) 145 146 /** 147 * WebGL API 的方法将给定的 WebGLTexture 绑定到目标(绑定点)。 148 */ 149 150 /** 151 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0004 152 * @tc.name webgl_test_bindTexture 153 * @tc.desc Test bindTexture. 154 */ 155 it('webgl_test_bindTexture', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 156 console.info("webgltest [webgl_test_bindTexture] bindTexture"); 157 let texture = gl.createTexture(); 158 console.info("webgltest ", "isTexture :", gl.isTexture(texture)); 159 expect(gl.isTexture(texture) == false).assertTrue(); 160 console.info("webgltest ", texture); 161 expect(texture != null).assertTrue(); 162 console.info("webgltest ", "gl.bindTexture(gl.TEXTURE_2D, texture);"); 163 gl.bindTexture(gl.TEXTURE_2D, texture); 164 console.info("webgltest ", "isTexture :", gl.isTexture(texture)); 165 expect(gl.isTexture(texture)).assertTrue(); 166 console.info("webgltest ", "gl.deleteTexture(texture);"); 167 gl.deleteTexture(texture); 168 console.info("webgltest ", "isTexture :", gl.isTexture(texture)); 169 expect(gl.isTexture(texture) == false).assertTrue(); 170 checkError(gl); 171 done(); 172 }) 173 174 /** 175 * WebGL API 的方法返回传递的参数名称的值。 176 */ 177 178 /** 179 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0005 180 * @tc.name webgl_test_activeTexture 181 * @tc.desc Test activeTexture. 182 */ 183 it('webgl_test_activeTexture', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 184 console.info("webgltest [webgl_test_activeTexture] activeTexture"); 185 let srcActiveTexture = gl.getParameter(gl.ACTIVE_TEXTURE); 186 gl.activeTexture(gl.TEXTURE0); 187 console.info("webgltest ACTIVE_TEXTURE:", gl.getParameter(gl.ACTIVE_TEXTURE)); 188 expect(gl.getParameter(gl.ACTIVE_TEXTURE)).assertEqual(gl.TEXTURE0); 189 console.info("webgltest gl.activeTexture(gl.TEXTURE12);"); 190 gl.activeTexture(gl.TEXTURE12); 191 console.info("webgltest ACTIVE_TEXTURE:", gl.getParameter(gl.ACTIVE_TEXTURE)); 192 expect(gl.getParameter(gl.ACTIVE_TEXTURE)).assertEqual(gl.TEXTURE12); 193 gl.activeTexture(srcActiveTexture); 194 checkError(gl); 195 done(); 196 }) 197 /** 198 * 返回有关给定纹理的信息 199 */ 200 201 /** 202 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0006 203 * @tc.name webgl_test_getTexParameter 204 * @tc.desc Test getTexParameter. 205 */ 206 it('webgl_test_getTexParameter', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 207 console.info("webgltest [webgl_test_getTexParameter] getTexParameter"); 208 let texture = gl.createTexture(); 209 gl.bindTexture(gl.TEXTURE_2D, texture); 210 console.info("webgltest gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER):", 211 gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER)); 212 expect(gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER) == gl.NEAREST_MIPMAP_LINEAR).assertTrue(); 213 console.info("webgltest gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)"); 214 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 215 console.info("webgltest gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER)", 216 gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER)); 217 expect(gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER) == gl.LINEAR).assertTrue(); 218 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR); 219 gl.deleteTexture(texture); 220 checkError(gl); 221 done(); 222 }) 223 224 /** 225 * 设置纹理参数 226 */ 227 228 /** 229 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0007 230 * @tc.name webgl_test_texParameteri 231 * @tc.desc Test texParameteri. 232 */ 233 it('webgl_test_texParameteri', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 234 console.info("webgltest [webgl_test_texParameteri] texParameteri"); 235 let texture = gl.createTexture(); 236 gl.bindTexture(gl.TEXTURE_2D, texture); 237 console.info("webgltest gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER):", 238 gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER)); 239 expect(gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER) == gl.NEAREST_MIPMAP_LINEAR).assertTrue(); 240 console.info("webgltest gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)"); 241 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 242 console.info("webgltest gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER)", 243 gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER)); 244 expect(gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER) == gl.LINEAR).assertTrue(); 245 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR); 246 gl.deleteTexture(texture); 247 checkError(gl); 248 done(); 249 }) 250 251 /** 252 * 设置纹理参数 253 */ 254 255 /** 256 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0008 257 * @tc.name webgl_test_texParameterf 258 * @tc.desc Test texParameterf. 259 */ 260 it('webgl_test_texParameterf', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 261 console.info("webgltest [webgl_test_texParameterf] texParameterf"); 262 let texture = gl.createTexture(); 263 gl.bindTexture(gl.TEXTURE_2D, texture); 264 console.info("webgltest gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER):", 265 gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER)); 266 expect(gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER) == gl.NEAREST_MIPMAP_LINEAR).assertTrue(); 267 console.info("webgltest gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)"); 268 gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 269 console.info("webgltest gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER)", 270 gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER)); 271 expect(gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER) == gl.LINEAR).assertTrue(); 272 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR); 273 gl.deleteTexture(texture); 274 checkError(gl); 275 done(); 276 }) 277 278 async function texImage2D(callback, finish) { 279 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 280 attribute vec4 a_Position; 281 attribute vec2 a_TexCoord; 282 varying vec2 v_TexCoord; 283 void main(){ 284 gl_Position = a_Position; 285 v_TexCoord = a_TexCoord; 286 } 287 `, ` 288 precision mediump float; 289 precision highp sampler2D; 290 uniform sampler2D u_Sampler; 291 varying vec2 v_TexCoord; 292 void main(){ 293 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 294 } 295 `); 296 let arr = new Float32Array([ 297 -0.5, 0.5, 0.0, 1.0, 298 -0.5, -0.5, 0.0, 0.0, 299 0.5, 0.5, 1.0, 1.0, 300 0.5, -0.5, 1.0, 0.0, 301 ]); 302 let FSIZE = arr.BYTES_PER_ELEMENT; 303 let buffer = gl.createBuffer(); // 创建缓冲区 304 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 305 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 306 307 let a_Position = gl.getAttribLocation(gl.program, 'a_Position'); // 获取attribute变量地址 308 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 309 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 310 311 let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord'); // 获取attribute变量地址 312 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 313 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 314 315 let u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler'); 316 let texture = gl.createTexture(); 317 318 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 对纹理图像进行y轴反转 319 gl.activeTexture(gl.TEXTURE0); // 开启0号纹理单元 320 gl.bindTexture(gl.TEXTURE_2D, texture); // 向target绑定纹理对象 321 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); // 配置纹理参数 322 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 配置纹理参数 323 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 配置纹理参数 324 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 配置纹理参数 325 const width = 4; 326 const height = 4; 327 const level = 0; 328 const internalFormat = gl.RGBA; 329 const format = gl.RGBA; 330 const type = gl.UNSIGNED_BYTE; 331 const data = new Uint8Array(width * height * 4); 332 for (let i = 0; i < data.length; i += 4) { 333 data[i] = 0; // 红色通道 334 data[i + 1] = 0; // 绿色通道 335 data[i + 2] = 255; // 蓝色通道 336 data[i + 3] = 255; // Alpha 通道 337 if (i > data.length / 2) { 338 data[i] = 255; 339 data[i + 1] = 0; 340 data[i + 2] = 0; 341 data[i + 3] = 255; 342 } 343 } 344 // 将图像数据加载到纹理中 345 callback(data); 346 finish(); 347 let err = checkError(gl); 348 expect(err).assertEqual(gl.NO_ERROR); 349 gl.uniform1i(u_Sampler, 0); 350 gl.clearColor(0.92, 0.92, 0.92, 1); 351 gl.clear(gl.COLOR_BUFFER_BIT); 352 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 353 gl.disableVertexAttribArray(a_Position); 354 gl.disableVertexAttribArray(a_TexCoord); 355 gl.bindTexture(gl.TEXTURE_2D, null); 356 gl.bindBuffer(gl.ARRAY_BUFFER, null); 357 gl.deleteBuffer(buffer); 358 gl.deleteTexture(texture); 359 gl.deleteShader(vertexShader); 360 gl.deleteShader(fragmentShader); 361 gl.deleteProgram(program); 362 gl.flush(); 363 } 364 365 366 /** 367 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0009 368 * @tc.name webgl_test_texImage2D 369 * @tc.desc Test texImage2D. 370 */ 371 it('webgl_test_texImage2D', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 372 console.info("webgltest [webgl_test_texImage2D] texImage2D"); 373 let image = await loadImage(); 374 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 375 attribute vec4 a_Position; 376 attribute vec2 a_TexCoord; 377 varying vec2 v_TexCoord; 378 void main(){ 379 gl_Position = a_Position; 380 v_TexCoord = a_TexCoord; 381 } 382 `, ` 383 precision mediump float; 384 precision highp sampler2D; 385 uniform sampler2D u_Sampler; 386 varying vec2 v_TexCoord; 387 void main(){ 388 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 389 } 390 `); 391 let arr = new Float32Array([ 392 -0.5, 0.5, 0.0, 1.0, 393 -0.5, -0.5, 0.0, 0.0, 394 0.5, 0.5, 1.0, 1.0, 395 0.5, -0.5, 1.0, 0.0, 396 ]); 397 let FSIZE = arr.BYTES_PER_ELEMENT; 398 let buffer = gl.createBuffer(); // 创建缓冲区 399 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 400 gl.bufferData(gl.ARRAY_BUFFER, arr.buffer, gl.STATIC_DRAW); // 将数据写入缓冲区对象 401 402 let a_Position = gl.getAttribLocation(gl.program, 'a_Position'); // 获取attribute变量地址 403 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 404 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 405 406 let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord'); // 获取attribute变量地址 407 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 408 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 409 410 let u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler'); 411 let texture = gl.createTexture(); 412 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 对纹理图像进行y轴反转 413 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 414 gl.activeTexture(gl.TEXTURE0); // 开启0号纹理单元 415 gl.bindTexture(gl.TEXTURE_2D, texture); // 向target绑定纹理对象 416 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); // 配置纹理参数 417 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 配置纹理参数 418 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 配置纹理参数 419 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 配置纹理参数 420 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image); // 配置纹理图像 421 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 422 gl.uniform1i(u_Sampler, 0); // 将0号纹理传递给着色器 423 gl.clearColor(0.92, 0.92, 0.92, 1); 424 gl.clear(gl.COLOR_BUFFER_BIT); 425 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 426 gl.flush(); 427 gl.disableVertexAttribArray(a_Position); 428 gl.disableVertexAttribArray(a_TexCoord); 429 gl.bindTexture(gl.TEXTURE_2D, null); 430 gl.bindBuffer(gl.ARRAY_BUFFER, null); 431 gl.deleteBuffer(buffer); 432 gl.deleteTexture(texture); 433 gl.deleteShader(vertexShader); 434 gl.deleteShader(fragmentShader); 435 gl.deleteProgram(program); 436 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 437 done(); 438 }) 439 440 /** 441 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0011 442 * @tc.name webgl_test_texImage2D_2 443 * @tc.desc Test texImage2D. 444 */ 445 it('webgl_test_texImage2D_2', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 446 console.info("webgltest [webgl_test_texImage2D_2] texImage2D"); 447 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 448 attribute vec4 a_Position; 449 attribute vec2 a_TexCoord; 450 varying vec2 v_TexCoord; 451 void main(){ 452 gl_Position = a_Position; 453 v_TexCoord = a_TexCoord; 454 } 455 `, ` 456 precision mediump float; 457 precision highp sampler2D; 458 uniform sampler2D u_Sampler; 459 varying vec2 v_TexCoord; 460 void main(){ 461 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 462 } 463 `); 464 let arr = new Float32Array([ 465 -0.5, 0.5, 0.0, 1.0, 466 -0.5, -0.5, 0.0, 0.0, 467 0.5, 0.5, 1.0, 1.0, 468 0.5, -0.5, 1.0, 0.0, 469 ]); 470 let FSIZE = arr.BYTES_PER_ELEMENT; 471 let buffer = gl.createBuffer(); // 创建缓冲区 472 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 473 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 474 475 let a_Position = gl.getAttribLocation(gl.program, 'a_Position'); // 获取attribute变量地址 476 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 477 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 478 479 let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord'); // 获取attribute变量地址 480 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 481 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 482 483 let u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler'); 484 let texture = gl.createTexture(); 485 486 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 对纹理图像进行y轴反转 487 gl.activeTexture(gl.TEXTURE0); // 开启0号纹理单元 488 gl.bindTexture(gl.TEXTURE_2D, texture); // 向target绑定纹理对象 489 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); // 配置纹理参数 490 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 配置纹理参数 491 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 配置纹理参数 492 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 配置纹理参数 493 const width = 4; 494 const height = 4; 495 const level = 0; 496 const internalFormat = gl.RGBA; 497 const format = gl.RGBA; 498 const type = gl.UNSIGNED_BYTE; 499 const data = new Uint8Array(width * height * 4); // 4个字节表示一个像素的 RGBA 值 500 for (let i = 0; i < data.length; i += 4) { 501 data[i] = 0; // 红色通道 502 data[i + 1] = 0; // 绿色通道 503 data[i + 2] = 255; // 蓝色通道 504 data[i + 3] = 255; // Alpha 通道 505 if (i > data.length / 2) { 506 data[i] = 255; 507 data[i + 1] = 0; 508 data[i + 2] = 0; 509 data[i + 3] = 255; 510 } 511 } 512 console.log(data); 513 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, 0, format, type, data); 514 let err = checkError(gl); 515 expect(err).assertEqual(gl.NO_ERROR); 516 gl.uniform1i(u_Sampler, 0); 517 gl.clearColor(0.92, 0.92, 0.92, 1); 518 gl.clear(gl.COLOR_BUFFER_BIT); 519 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 520 gl.disableVertexAttribArray(a_Position); 521 gl.disableVertexAttribArray(a_TexCoord); 522 gl.bindTexture(gl.TEXTURE_2D, null); 523 gl.bindBuffer(gl.ARRAY_BUFFER, null); 524 gl.deleteBuffer(buffer); 525 gl.deleteTexture(texture); 526 gl.deleteShader(vertexShader); 527 gl.deleteShader(fragmentShader); 528 gl.deleteProgram(program); 529 gl.flush(); 530 done(); 531 }) 532 533 534 /** 535 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0012 536 * @tc.name webgl_test_texImage2D_3 537 * @tc.desc Test texImage2D. 538 */ 539 it('webgl_test_texImage2D_3', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 540 console.info("webgltest [webgl_test_texImage2D_3] texImage2D"); 541 await texImage2D((image2D) => { 542 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 4, 4, 0, gl.RGBA, gl.UNSIGNED_BYTE, image2D); 543 }, () => { 544 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 545 }) 546 done(); 547 }) 548 549 550 /** 551 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0013 552 * @tc.name webgl_test_texImage2D_4 553 * @tc.desc Test texImage2D. 554 */ 555 it('webgl_test_texImage2D_4', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 556 console.info("webgltest [webgl_test_texImage2D_4] texImage2D"); 557 await texImage2D((image2D) => { 558 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 4, 4, 0, gl.RGBA, gl.UNSIGNED_BYTE, image2D); 559 }, () => { 560 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 561 }) 562 done(); 563 }) 564 565 566 /** 567 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0014 568 * @tc.name webgl_test_texImage2D_5 569 * @tc.desc Test texImage2D. 570 */ 571 it('webgl_test_texImage2D_5', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 572 console.info("webgltest [webgl_test_texImage2D_5] texImage2D"); 573 await texImage2D((image2D) => { 574 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 4, 4, 0, gl.RGBA, gl.UNSIGNED_BYTE, image2D); 575 }, () => { 576 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 577 }) 578 done(); 579 }) 580 581 582 /** 583 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0015 584 * @tc.name webgl_test_texImage2D_6 585 * @tc.desc Test texImage2D. 586 */ 587 it('webgl_test_texImage2D_6', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 588 console.info("webgltest [webgl_test_texImage2D_6] texImage2D"); 589 await texImage2D((image2D) => { 590 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 4, 4, 0, gl.RGBA, gl.UNSIGNED_BYTE, image2D); 591 }, () => { 592 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 593 }); 594 done(); 595 }) 596 597 598 /** 599 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0016 600 * @tc.name webgl_test_texImage2D_7 601 * @tc.desc Test texImage2D. 602 */ 603 it('webgl_test_texImage2D_7', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 604 console.info("webgltest [webgl_test_texImage2D_7] texImage2D"); 605 await texImage2D((image2D) => { 606 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 4, 4, 0, gl.RGBA, gl.UNSIGNED_BYTE, image2D); 607 }, () => { 608 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 609 }) 610 done(); 611 612 }) 613 614 615 /** 616 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0017 617 * @tc.name webgl_test_texImage2D_8 618 * @tc.desc Test texImage2D. 619 */ 620 it('webgl_test_texImage2D_8', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 621 console.info("webgltest [webgl_test_texImage2D_8] texImage2D"); 622 await texImage2D((image2D) => { 623 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 4, 4, 0, gl.RGBA, gl.UNSIGNED_BYTE, image2D); 624 }, () => { 625 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 626 }) 627 done(); 628 629 }) 630 631 632 /** 633 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0018 634 * @tc.name webgl_test_texImage2D_9 635 * @tc.desc Test texImage2D. 636 */ 637 it('webgl_test_texImage2D_9', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 638 console.info("webgltest [webgl_test_texImage2D_9] texImage2D"); 639 await texImage2D((image2D) => { 640 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 4, 4, 0, gl.RGB, gl.UNSIGNED_BYTE, image2D); 641 }, () => { 642 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 643 }) 644 done(); 645 646 }) 647 648 649 /** 650 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0019 651 * @tc.name webgl_test_texImage2D_10 652 * @tc.desc Test texImage2D. 653 */ 654 it('webgl_test_texImage2D_10', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 655 console.info("webgltest [webgl_test_texImage2D_10] texImage2D"); 656 await texImage2D((image2D) => { 657 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 4, 4, 0, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, image2D); 658 }, () => { 659 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 660 }) 661 done(); 662 }) 663 664 665 /** 666 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0020 667 * @tc.name webgl_test_texImage2D_11 668 * @tc.desc Test texImage2D. 669 */ 670 it('webgl_test_texImage2D_11', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 671 console.info("webgltest [webgl_test_texImage2D_11] texImage2D"); 672 await texImage2D((image2D) => { 673 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 4, 4, 0, gl.RGB, gl.UNSIGNED_SHORT_4_4_4_4, image2D); 674 }, () => { 675 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 676 }) 677 done(); 678 }) 679 680 681 /** 682 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0021 683 * @tc.name webgl_test_texImage2D_12 684 * @tc.desc Test texImage2D. 685 */ 686 it('webgl_test_texImage2D_12', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 687 console.info("webgltest [webgl_test_texImage2D_12] texImage2D"); 688 await texImage2D((image2D) => { 689 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 4, 4, 0, gl.RGB, gl.UNSIGNED_SHORT_5_5_5_1, image2D); 690 }, () => { 691 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 692 }) 693 done(); 694 }) 695 696 697 /** 698 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0022 699 * @tc.name webgl_test_texImage2D_Error 700 * @tc.desc Test texImage2D. 701 */ 702 it('webgl_test_texImage2D_Error', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 703 console.info("webgltest [webgl_test_texImage2D_Error] texImage2D"); 704 let width = 256; 705 let height = 256; 706 let level = 0; 707 let internalFormat = gl.RGBA; 708 let format = gl.RGBA; 709 let type = gl.UNSIGNED_BYTE; 710 let data = new Uint8Array(width * height * 4); 711 for (let i = 0; i < data.length; i += 4) { 712 data[i] = 0; // 红色通道 713 data[i + 1] = 0; // 绿色通道 714 data[i + 2] = 255; // 蓝色通道 715 data[i + 3] = 255; // Alpha 通道 716 } 717 console.info("webgltest [webgl_test_texImage2D] texImage2D no target"); 718 gl.texImage2D(undefined, level, internalFormat, width, height, 0, format, type, data); 719 checkError(gl) 720 console.info("webgltest [webgl_test_texImage2D] texImage2D no level"); 721 level = undefined 722 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, 0, format, type, data); 723 checkError(gl) 724 level = 0 725 console.info("webgltest [webgl_test_texImage2D] texImage2D no internalFormat"); 726 internalFormat = undefined 727 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, 0, format, type, data); 728 checkError(gl) 729 width = undefined 730 console.info("webgltest [webgl_test_texImage2D] texImage2D no width"); 731 internalFormat = undefined 732 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, 0, format, type, data); 733 checkError(gl) 734 internalFormat = 256 735 console.info("webgltest [webgl_test_texImage2D] texImage2D no height"); 736 height = undefined 737 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, 0, format, type, data); 738 checkError(gl) 739 height = 256 740 console.info("webgltest [webgl_test_texImage2D] texImage2D no border"); 741 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, undefined, format, type, data); 742 checkError(gl) 743 console.info("webgltest [webgl_test_texImage2D] texImage2D no format"); 744 format = undefined 745 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, 0, format, type, data); 746 checkError(gl) 747 format = gl.RGBA; 748 console.info("webgltest [webgl_test_texImage2D] texImage2D no type"); 749 type = undefined 750 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, 0, format, type, data); 751 checkError(gl) 752 type = gl.UNSIGNED_BYTE; 753 console.info("webgltest [webgl_test_texImage2D] texImage2D no data"); 754 data = undefined 755 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, 0, format, type, data); 756 checkError(gl) 757 done(); 758 }) 759 760 761 /** 762 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0023 763 * @tc.name webgl_test_generateMipmap 764 * @tc.desc Test generateMipmap. 765 */ 766 it('webgl_test_generateMipmap', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 767 console.info("webgltest [generateMipmap] generateMipmap"); 768 gl.generateMipmap(gl.TEXTURE_2D); 769 const error = checkError(gl); 770 expect(error).assertEqual(gl.INVALID_OPERATION); 771 done(); 772 }) 773 774 async function texSubImage2D(callback, finish) { 775 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 776 attribute vec4 a_Position; 777 attribute vec2 a_TexCoord; 778 varying vec2 v_TexCoord; 779 void main(){ 780 gl_Position = a_Position; 781 v_TexCoord = a_TexCoord; 782 } 783 `, ` 784 precision mediump float; 785 precision highp sampler2D; 786 uniform sampler2D u_Sampler; 787 varying vec2 v_TexCoord; 788 void main(){ 789 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 790 } 791 `); 792 let arr = new Float32Array([ 793 -0.5, 0.5, 0.0, 1.0, 794 -0.5, -0.5, 0.0, 0.0, 795 0.5, 0.5, 1.0, 1.0, 796 0.5, -0.5, 1.0, 0.0, 797 ]); 798 let FSIZE = arr.BYTES_PER_ELEMENT; 799 let buffer = gl.createBuffer(); // 创建缓冲区 800 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 801 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 802 803 let a_Position = gl.getAttribLocation(gl.program, 'a_Position'); // 获取attribute变量地址 804 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 805 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 806 807 let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord'); // 获取attribute变量地址 808 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 809 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 810 811 let u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler'); 812 let texture = gl.createTexture(); 813 814 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 对纹理图像进行y轴反转 815 gl.activeTexture(gl.TEXTURE0); // 开启0号纹理单元 816 gl.bindTexture(gl.TEXTURE_2D, texture); // 向target绑定纹理对象 817 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); // 配置纹理参数 818 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 配置纹理参数 819 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 配置纹理参数 820 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 配置纹理参数 821 const width = 256; 822 const height = 256; 823 const level = 0; 824 const internalFormat = gl.RGBA; 825 const border = 0; 826 const format = gl.RGBA; 827 const type = gl.UNSIGNED_BYTE; 828 const data = new Uint8Array(width * height * 4); 829 for (let i = 0; i < data.length; i += 4) { 830 data[i] = 0; // 红色通道 831 data[i + 1] = 0; // 绿色通道 832 data[i + 2] = 255; // 蓝色通道 833 data[i + 3] = 255; // Alpha 通道 834 } 835 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, format, type, data); 836 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 837 gl.uniform1i(u_Sampler, 0); 838 gl.clearColor(0.92, 0.92, 0.92, 1); 839 gl.clear(gl.COLOR_BUFFER_BIT); 840 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 841 842 const newData = new Uint8Array(width * height * 4); 843 for (let i = 0; i < newData.length; i += 4) { 844 newData[i] = 255; // 红色通道 845 newData[i + 1] = 0; // 绿色通道 846 newData[i + 2] = 0; // 蓝色通道 847 newData[i + 3] = 255; // Alpha 通道 848 } 849 const xOffset = 128; 850 const yOffset = 64; 851 callback(newData) 852 finish() 853 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 854 gl.clearColor(0.92, 0.92, 0.92, 1); 855 gl.clear(gl.COLOR_BUFFER_BIT); 856 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 857 858 gl.disableVertexAttribArray(a_Position); 859 gl.disableVertexAttribArray(a_TexCoord); 860 gl.bindTexture(gl.TEXTURE_2D, null); 861 gl.bindBuffer(gl.ARRAY_BUFFER, null); 862 gl.deleteBuffer(buffer); 863 gl.deleteTexture(texture); 864 gl.deleteShader(vertexShader); 865 gl.deleteShader(fragmentShader); 866 gl.deleteProgram(program); 867 gl.flush(); 868 } 869 870 871 /** 872 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0024 873 * @tc.name webgl_test_texSubImage2D 874 * @tc.desc Test texSubImage2D. 875 */ 876 it('webgl_test_texSubImage2D', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 877 console.info("webgltest [webgl_test_texSubImage2D] texSubImage2D"); 878 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 879 attribute vec4 a_Position; 880 attribute vec2 a_TexCoord; 881 varying vec2 v_TexCoord; 882 void main(){ 883 gl_Position = a_Position; 884 v_TexCoord = a_TexCoord; 885 } 886 `, ` 887 precision mediump float; 888 precision highp sampler2D; 889 uniform sampler2D u_Sampler; 890 varying vec2 v_TexCoord; 891 void main(){ 892 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 893 } 894 `); 895 let arr = new Float32Array([ 896 -0.5, 0.5, 0.0, 1.0, 897 -0.5, -0.5, 0.0, 0.0, 898 0.5, 0.5, 1.0, 1.0, 899 0.5, -0.5, 1.0, 0.0, 900 ]); 901 let FSIZE = arr.BYTES_PER_ELEMENT; 902 let buffer = gl.createBuffer(); // 创建缓冲区 903 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 904 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 905 906 let a_Position = gl.getAttribLocation(gl.program, 'a_Position'); // 获取attribute变量地址 907 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 908 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 909 910 let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord'); // 获取attribute变量地址 911 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 912 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 913 914 let u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler'); 915 let texture = gl.createTexture(); 916 917 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 对纹理图像进行y轴反转 918 gl.activeTexture(gl.TEXTURE0); // 开启0号纹理单元 919 gl.bindTexture(gl.TEXTURE_2D, texture); // 向target绑定纹理对象 920 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); // 配置纹理参数 921 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 配置纹理参数 922 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 配置纹理参数 923 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 配置纹理参数 924 const width = 256; 925 const height = 256; 926 const level = 0; 927 const internalFormat = gl.RGBA; 928 const border = 0; 929 const format = gl.RGBA; 930 const type = gl.UNSIGNED_BYTE; 931 const data = new Uint8Array(width * height * 4); 932 for (let i = 0; i < data.length; i += 4) { 933 data[i] = 0; // 红色通道 934 data[i + 1] = 0; // 绿色通道 935 data[i + 2] = 255; // 蓝色通道 936 data[i + 3] = 255; // Alpha 通道 937 } 938 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, format, type, data); 939 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 940 gl.uniform1i(u_Sampler, 0); 941 gl.clearColor(0.92, 0.92, 0.92, 1); 942 gl.clear(gl.COLOR_BUFFER_BIT); 943 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 944 945 const newData = new Uint8Array(width * height * 4); 946 for (let i = 0; i < newData.length; i += 4) { 947 newData[i] = 255; // 红色通道 948 newData[i + 1] = 0; // 绿色通道 949 newData[i + 2] = 0; // 蓝色通道 950 newData[i + 3] = 255; // Alpha 通道 951 } 952 const xOffset = 128; 953 const yOffset = 64; 954 gl.texSubImage2D(gl.TEXTURE_2D, level, xOffset, yOffset, width - 128, height - 64, format, type, newData); 955 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 956 gl.clearColor(0.92, 0.92, 0.92, 1); 957 gl.clear(gl.COLOR_BUFFER_BIT); 958 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 959 960 gl.disableVertexAttribArray(a_Position); 961 gl.disableVertexAttribArray(a_TexCoord); 962 gl.bindTexture(gl.TEXTURE_2D, null); 963 gl.bindBuffer(gl.ARRAY_BUFFER, null); 964 gl.deleteBuffer(buffer); 965 gl.deleteTexture(texture); 966 gl.deleteShader(vertexShader); 967 gl.deleteShader(fragmentShader); 968 gl.deleteProgram(program); 969 gl.flush(); 970 done(); 971 }) 972 973 974 /** 975 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0025 976 * @tc.name webgl_test_texSubImage2D_1 977 * @tc.desc Test texSubImage2D. 978 */ 979 it('webgl_test_texSubImage2D_1', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 980 console.info("webgltest [webgl_test_texSubImage2D_1] texSubImage2D"); 981 await texSubImage2D((data) => { 982 const width = 256; 983 const height = 256; 984 const level = 0; 985 const format = gl.RGBA; 986 const type = gl.UNSIGNED_BYTE; 987 const xOffset = 128; 988 const yOffset = 64; 989 gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X, 990 level, xOffset, yOffset, width - 128, height - 64, format, type, data); 991 }, () => { 992 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 993 }) 994 done() 995 }) 996 997 998 /** 999 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0026 1000 * @tc.name webgl_test_texSubImage2D_14 1001 * @tc.desc Test texSubImage2D. 1002 */ 1003 it('webgl_test_texSubImage2D_14', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1004 console.info("webgltest [webgl_test_texSubImage2D_14] texSubImage2D"); 1005 await texSubImage2D((data) => { 1006 const width = 256; 1007 const height = 256; 1008 const level = 0; 1009 const format = gl.RGBA; 1010 const type = gl.UNSIGNED_BYTE; 1011 const xOffset = 128; 1012 const yOffset = 64; 1013 gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 1014 level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1015 }, () => { 1016 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1017 }) 1018 done() 1019 }) 1020 1021 1022 /** 1023 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0027 1024 * @tc.name webgl_test_texSubImage2D_3 1025 * @tc.desc Test texSubImage2D. 1026 */ 1027 it('webgl_test_texSubImage2D_3', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1028 console.info("webgltest [webgl_test_texSubImage2D_3] texSubImage2D"); 1029 await texSubImage2D((data) => { 1030 const width = 256; 1031 const height = 256; 1032 const level = 0; 1033 const format = gl.RGBA; 1034 const type = gl.UNSIGNED_BYTE; 1035 const xOffset = 128; 1036 const yOffset = 64; 1037 gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 1038 level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1039 }, () => { 1040 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1041 }) 1042 done() 1043 }) 1044 1045 1046 /** 1047 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0028 1048 * @tc.name webgl_test_texSubImage2D_4 1049 * @tc.desc Test texSubImage2D. 1050 */ 1051 it('webgl_test_texSubImage2D_4', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1052 console.info("webgltest [webgl_test_texSubImage2D_4] texSubImage2D"); 1053 await texSubImage2D((data) => { 1054 const width = 256; 1055 const height = 256; 1056 const level = 0; 1057 const format = gl.RGBA; 1058 const type = gl.UNSIGNED_BYTE; 1059 const xOffset = 128; 1060 const yOffset = 64; 1061 gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 1062 level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1063 }, () => { 1064 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1065 }) 1066 done() 1067 }) 1068 1069 1070 /** 1071 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0029 1072 * @tc.name webgl_test_texSubImage2D_5 1073 * @tc.desc Test texSubImage2D. 1074 */ 1075 it('webgl_test_texSubImage2D_5', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1076 console.info("webgltest [webgl_test_texSubImage2D_5] texSubImage2D"); 1077 await texSubImage2D((data) => { 1078 const width = 256; 1079 const height = 256; 1080 const level = 0; 1081 const format = gl.RGBA; 1082 const type = gl.UNSIGNED_BYTE; 1083 const xOffset = 128; 1084 const yOffset = 64; 1085 gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 1086 level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1087 }, () => { 1088 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1089 }) 1090 done() 1091 }) 1092 1093 1094 /** 1095 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0030 1096 * @tc.name webgl_test_texSubImage2D_6 1097 * @tc.desc Test texSubImage2D. 1098 */ 1099 it('webgl_test_texSubImage2D_6', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1100 console.info("webgltest [webgl_test_texSubImage2D_6] texSubImage2D"); 1101 await texSubImage2D((data) => { 1102 const width = 256; 1103 const height = 256; 1104 const level = 0; 1105 const format = gl.RGBA; 1106 const type = gl.UNSIGNED_BYTE; 1107 const xOffset = 128; 1108 const yOffset = 64; 1109 gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, 1110 level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1111 }, () => { 1112 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1113 }) 1114 done() 1115 }) 1116 1117 1118 /** 1119 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0031 1120 * @tc.name webgl_test_texSubImage2D_7 1121 * @tc.desc Test texSubImage2D. 1122 */ 1123 it('webgl_test_texSubImage2D_7', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1124 console.info("webgltest [webgl_test_texSubImage2D_7] texSubImage2D"); 1125 await texSubImage2D((data) => { 1126 const width = 256; 1127 const height = 256; 1128 const level = 0; 1129 const format = gl.ALPHA; 1130 const type = gl.UNSIGNED_BYTE; 1131 const xOffset = 128; 1132 const yOffset = 64; 1133 gl.texSubImage2D(gl.TEXTURE_2D, level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1134 }, () => { 1135 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1136 }) 1137 done() 1138 }) 1139 1140 1141 /** 1142 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0032 1143 * @tc.name webgl_test_texSubImage2D_8 1144 * @tc.desc Test texSubImage2D. 1145 */ 1146 it('webgl_test_texSubImage2D_8', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1147 console.info("webgltest [webgl_test_texSubImage2D_8] texSubImage2D"); 1148 await texSubImage2D((data) => { 1149 const width = 256; 1150 const height = 256; 1151 const level = 0; 1152 const format = gl.RGB; 1153 const type = gl.UNSIGNED_BYTE; 1154 const xOffset = 128; 1155 const yOffset = 64; 1156 gl.texSubImage2D(gl.TEXTURE_2D, level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1157 }, () => { 1158 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1159 }) 1160 done() 1161 }) 1162 1163 1164 /** 1165 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0033 1166 * @tc.name webgl_test_texSubImage2D_9 1167 * @tc.desc Test texSubImage2D. 1168 */ 1169 it('webgl_test_texSubImage2D_9', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1170 console.info("webgltest [webgl_test_texSubImage2D_9] texSubImage2D"); 1171 await texSubImage2D((data) => { 1172 const width = 256; 1173 const height = 256; 1174 const level = 0; 1175 const format = gl.LUMINANCE; 1176 const type = gl.UNSIGNED_BYTE; 1177 const xOffset = 128; 1178 const yOffset = 64; 1179 gl.texSubImage2D(gl.TEXTURE_2D, level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1180 }, () => { 1181 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1182 }) 1183 done() 1184 }) 1185 1186 1187 /** 1188 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0034 1189 * @tc.name webgl_test_texSubImage2D_10 1190 * @tc.desc Test texSubImage2D. 1191 */ 1192 it('webgl_test_texSubImage2D_10', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1193 console.info("webgltest [webgl_test_texSubImage2D_10] texSubImage2D"); 1194 await texSubImage2D((data) => { 1195 const width = 256; 1196 const height = 256; 1197 const level = 0; 1198 const format = gl.LUMINANCE_ALPHA; 1199 const type = gl.UNSIGNED_BYTE; 1200 const xOffset = 128; 1201 const yOffset = 64; 1202 gl.texSubImage2D(gl.TEXTURE_2D, level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1203 }, () => { 1204 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1205 }) 1206 done() 1207 }) 1208 1209 1210 /** 1211 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0035 1212 * @tc.name webgl_test_texSubImage2D_11 1213 * @tc.desc Test texSubImage2D. 1214 */ 1215 it('webgl_test_texSubImage2D_11', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1216 console.info("webgltest [webgl_test_texSubImage2D_11] texSubImage2D"); 1217 await texSubImage2D((data) => { 1218 const width = 256; 1219 const height = 256; 1220 const level = 0; 1221 const format = gl.RGBA; 1222 const type = gl.UNSIGNED_SHORT_5_6_5; 1223 const xOffset = 128; 1224 const yOffset = 64; 1225 gl.texSubImage2D(gl.TEXTURE_2D, level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1226 }, () => { 1227 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1228 }) 1229 done() 1230 }) 1231 1232 1233 /** 1234 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0036 1235 * @tc.name webgl_test_texSubImage2D_12 1236 * @tc.desc Test texSubImage2D. 1237 */ 1238 it('webgl_test_texSubImage2D_12', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1239 console.info("webgltest [webgl_test_texSubImage2D_12] texSubImage2D"); 1240 await texSubImage2D((data) => { 1241 const width = 256; 1242 const height = 256; 1243 const level = 0; 1244 const format = gl.RGBA; 1245 const type = gl.UNSIGNED_SHORT_4_4_4_4; 1246 const xOffset = 128; 1247 const yOffset = 64; 1248 gl.texSubImage2D(gl.TEXTURE_2D, level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1249 }, () => { 1250 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1251 }) 1252 done() 1253 }) 1254 1255 1256 /** 1257 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0037 1258 * @tc.name webgl_test_texSubImage2D_13 1259 * @tc.desc Test texSubImage2D. 1260 */ 1261 it('webgl_test_texSubImage2D_13', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1262 console.info("webgltest [webgl_test_texSubImage2D_13] texSubImage2D"); 1263 await texSubImage2D((data) => { 1264 const width = 256; 1265 const height = 256; 1266 const level = 0; 1267 const format = gl.RGBA; 1268 const type = gl.UNSIGNED_SHORT_5_5_5_1; 1269 const xOffset = 128; 1270 const yOffset = 64; 1271 gl.texSubImage2D(gl.TEXTURE_2D, level, xOffset, yOffset, width - 128, height - 64, format, type, data); 1272 }, () => { 1273 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1274 }) 1275 done() 1276 }) 1277 1278 function loadFile(src) { 1279 return new Promise((resolve, reject) => { 1280 fetch(src).then(rs => { 1281 return rs.blob() 1282 }).then(rs => { 1283 const imageUrl = URL.createObjectURL(rs); 1284 let image = new Image(); 1285 image.onload = ev1 => { 1286 let imageBitmap = createImageBitmap(image, 0, 0, image.width, image.height); 1287 console.log(imageBitmap); 1288 resolve(imageBitmap) 1289 } 1290 image.src = imageUrl; 1291 }) 1292 }); 1293 } 1294 1295 function loadImage(src) { 1296 return new Promise((resolve, reject) => { 1297 let image = new Image(); 1298 resolve(image); 1299 image.src = `data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==`; 1300 }); 1301 } 1302 1303 1304 /** 1305 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0038 1306 * @tc.name webgl_test_texSubImage2D_2 1307 * @tc.desc Test texSubImage2D. 1308 */ 1309 it('webgl_test_texSubImage2D_2', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 1310 console.info("webgltest [webgl_test_texSubImage2D_2] texSubImage2D"); 1311 let image = await loadImage() 1312 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 1313 attribute vec4 a_Position; 1314 attribute vec2 a_TexCoord; 1315 varying vec2 v_TexCoord; 1316 void main(){ 1317 gl_Position = a_Position; 1318 v_TexCoord = a_TexCoord; 1319 } 1320 `, ` 1321 precision mediump float; 1322 precision highp sampler2D; 1323 uniform sampler2D u_Sampler; 1324 varying vec2 v_TexCoord; 1325 void main(){ 1326 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 1327 } 1328 `); 1329 let arr = new Float32Array([ 1330 -0.5, 0.5, 0.0, 1.0, 1331 -0.5, -0.5, 0.0, 0.0, 1332 0.5, 0.5, 1.0, 1.0, 1333 0.5, -0.5, 1.0, 0.0, 1334 ]); 1335 let FSIZE = arr.BYTES_PER_ELEMENT; 1336 let buffer = gl.createBuffer(); // 创建缓冲区 1337 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 1338 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 1339 1340 let a_Position = gl.getAttribLocation(gl.program, 'a_Position'); // 获取attribute变量地址 1341 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 1342 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 1343 1344 let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord'); // 获取attribute变量地址 1345 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 1346 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 1347 1348 let u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler'); 1349 let texture = gl.createTexture(); 1350 1351 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 对纹理图像进行y轴反转 1352 gl.activeTexture(gl.TEXTURE0); // 开启0号纹理单元 1353 gl.bindTexture(gl.TEXTURE_2D, texture); // 向target绑定纹理对象 1354 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); // 配置纹理参数 1355 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 配置纹理参数 1356 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 配置纹理参数 1357 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 配置纹理参数 1358 const width = 500; 1359 const height = 500; 1360 const level = 0; 1361 const internalFormat = gl.RGBA; 1362 const border = 0; 1363 const format = gl.RGBA; 1364 const type = gl.UNSIGNED_BYTE; 1365 const data = new Uint8Array(width * height * 4); 1366 for (let i = 0; i < data.length; i += 4) { 1367 data[i] = 0; // 红色通道 1368 data[i + 1] = 0; // 绿色通道 1369 data[i + 2] = 255; // 蓝色通道 1370 data[i + 3] = 255; // Alpha 通道 1371 } 1372 gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, format, type, data); 1373 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1374 gl.uniform1i(u_Sampler, 0); 1375 gl.clearColor(0.92, 0.92, 0.92, 1); 1376 gl.clear(gl.COLOR_BUFFER_BIT); 1377 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 1378 1379 console.log(image); 1380 gl.texSubImage2D(gl.TEXTURE_2D, level, 490, 490, format, type, image); 1381 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1382 gl.texSubImage2D(gl.TEXTURE_2D, level, 400, 400, format, type, image); 1383 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1384 gl.texSubImage2D(gl.TEXTURE_2D, level, 300, 300, format, type, image); 1385 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1386 gl.texSubImage2D(gl.TEXTURE_2D, level, 200, 200, format, type, image); 1387 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1388 gl.texSubImage2D(gl.TEXTURE_2D, level, 100, 100, format, type, image); 1389 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1390 gl.clearColor(0.92, 0.92, 0.92, 1); 1391 gl.clear(gl.COLOR_BUFFER_BIT); 1392 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 1393 gl.disableVertexAttribArray(a_Position); 1394 gl.disableVertexAttribArray(a_TexCoord); 1395 gl.bindTexture(gl.TEXTURE_2D, null); 1396 gl.bindBuffer(gl.ARRAY_BUFFER, null); 1397 gl.deleteBuffer(buffer); 1398 gl.deleteTexture(texture); 1399 gl.deleteShader(vertexShader); 1400 gl.deleteShader(fragmentShader); 1401 gl.deleteProgram(program); 1402 gl.flush(); 1403 done(); 1404 }) 1405 1406 1407 /** 1408 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0039 1409 * @tc.name webgl_test_texSubImage2D_Error 1410 * @tc.desc Test texSubImage2D. 1411 */ 1412 it('webgl_test_texSubImage2D_Error', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1413 async function (done) { 1414 console.info("webgltest [webgl_test_texSubImage2D] texSubImage2D"); 1415 let image = await loadImage() 1416 const width = 500; 1417 const height = 500; 1418 const level = 0; 1419 const internalFormat = gl.RGBA; 1420 const border = 0; 1421 const format = gl.RGBA; 1422 const type = gl.UNSIGNED_BYTE; 1423 const data = new Uint8Array(width * height * 4); 1424 for (let i = 0; i < data.length; i += 4) { 1425 data[i] = 0; // 红色通道 1426 data[i + 1] = 0; // 绿色通道 1427 data[i + 2] = 255; // 蓝色通道 1428 data[i + 3] = 255; // Alpha 通道 1429 } 1430 console.info("webgltest [webgl_test_texSubImage2D] texSubImage2D no target"); 1431 gl.texSubImage2D(undefined, level, 490, 490, format, type, image); 1432 checkError(gl) 1433 console.info("webgltest [webgl_test_texSubImage2D] texSubImage2D no level"); 1434 gl.texSubImage2D(gl.TEXTURE_2D, undefined, 490, 490, format, type, image); 1435 checkError(gl) 1436 console.info("webgltest [webgl_test_texSubImage2D] texSubImage2D no xoffset"); 1437 gl.texSubImage2D(gl.TEXTURE_2D, level, undefined, 490, format, type, image); 1438 checkError(gl) 1439 console.info("webgltest [webgl_test_texSubImage2D] texSubImage2D no yoffset"); 1440 gl.texSubImage2D(gl.TEXTURE_2D, level, 490, undefined, format, type, image); 1441 checkError(gl) 1442 console.info("webgltest [webgl_test_texSubImage2D] texSubImage2D no format"); 1443 gl.texSubImage2D(gl.TEXTURE_2D, level, 490, 490, undefined, type, image); 1444 checkError(gl) 1445 console.info("webgltest [webgl_test_texSubImage2D] texSubImage2D no type"); 1446 gl.texSubImage2D(gl.TEXTURE_2D, level, 490, 490, format, undefined, image); 1447 checkError(gl) 1448 done() 1449 }) 1450 1451 1452 function compressedTexImage2D(callback, finish) { 1453 console.info("webgltest [webgl_test_compressedTexImage2D] compressedTexImage2D"); 1454 var availableExtensions = gl.getSupportedExtensions(); 1455 for (var i = 0; i < availableExtensions.length; i++) { 1456 if (availableExtensions[i].indexOf('texture') >= 0 1457 && availableExtensions[i].indexOf('compressed') >= 0) { 1458 console.log(availableExtensions[i]); 1459 } 1460 } 1461 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 1462 attribute vec4 a_Position; 1463 attribute vec2 a_TexCoord; 1464 varying vec2 v_TexCoord; 1465 void main(){ 1466 gl_Position = a_Position; 1467 v_TexCoord = a_TexCoord; 1468 } 1469 `, ` 1470 precision mediump float; 1471 precision highp sampler2D; 1472 uniform sampler2D u_Sampler; 1473 varying vec2 v_TexCoord; 1474 void main(){ 1475 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 1476 } 1477 `); 1478 let arr = new Float32Array([ 1479 -0.5, 0.5, 0.0, 1.0, 1480 -0.5, -0.5, 0.0, 0.0, 1481 0.5, 0.5, 1.0, 1.0, 1482 0.5, -0.5, 1.0, 0.0, 1483 ]); 1484 const ext = 1485 gl.getExtension("WEBGL_compressed_texture_s3tc") || 1486 gl.getExtension("MOZ_WEBGL_compressed_texture_s3tc") || 1487 gl.getExtension("WEBKIT_WEBGL_compressed_texture_s3tc") || {}; 1488 1489 let FSIZE = arr.BYTES_PER_ELEMENT; 1490 let buffer = gl.createBuffer(); // 创建缓冲区 1491 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 1492 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 1493 1494 let a_Position = gl.getAttribLocation(gl.program, 'a_Position'); // 获取attribute变量地址 1495 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 1496 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 1497 1498 let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord'); // 获取attribute变量地址 1499 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 1500 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 1501 1502 let u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler'); 1503 let texture = gl.createTexture(); 1504 1505 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 对纹理图像进行y轴反转 1506 gl.activeTexture(gl.TEXTURE0); // 开启0号纹理单元 1507 gl.bindTexture(gl.TEXTURE_2D, texture); // 向target绑定纹理对象 1508 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); // 配置纹理参数 1509 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 配置纹理参数 1510 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 配置纹理参数 1511 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 配置纹理参数 1512 1513 callback(ext) 1514 finish() 1515 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1516 gl.uniform1i(u_Sampler, 0); 1517 gl.clearColor(0.92, 0.92, 0.92, 1); 1518 gl.clear(gl.COLOR_BUFFER_BIT); 1519 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 1520 gl.disableVertexAttribArray(a_Position); 1521 gl.disableVertexAttribArray(a_TexCoord); 1522 gl.bindTexture(gl.TEXTURE_2D, null); 1523 gl.bindBuffer(gl.ARRAY_BUFFER, null); 1524 gl.deleteBuffer(buffer); 1525 gl.deleteTexture(texture); 1526 gl.deleteShader(vertexShader); 1527 gl.deleteShader(fragmentShader); 1528 gl.deleteProgram(program); 1529 gl.flush(); 1530 } 1531 1532 1533 /** 1534 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0040 1535 * @tc.name webgl_test_compressedTexImage2D 1536 * @tc.desc Test compressedTexImage2D. 1537 */ 1538 it('webgl_test_compressedTexImage2D', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1539 async function (done) { 1540 console.info("webgltest [webgl_test_compressedTexImage2D] compressedTexImage2D"); 1541 var availableExtensions = gl.getSupportedExtensions(); 1542 for (var i = 0; i < availableExtensions.length; i++) { 1543 if (availableExtensions[i].indexOf('texture') >= 0 1544 && availableExtensions[i].indexOf('compressed') >= 0) { 1545 console.log(availableExtensions[i]); 1546 } 1547 } 1548 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 1549 attribute vec4 a_Position; 1550 attribute vec2 a_TexCoord; 1551 varying vec2 v_TexCoord; 1552 void main(){ 1553 gl_Position = a_Position; 1554 v_TexCoord = a_TexCoord; 1555 } 1556 `, ` 1557 precision mediump float; 1558 precision highp sampler2D; 1559 uniform sampler2D u_Sampler; 1560 varying vec2 v_TexCoord; 1561 void main(){ 1562 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 1563 } 1564 `); 1565 let arr = new Float32Array([ 1566 -0.5, 0.5, 0.0, 1.0, 1567 -0.5, -0.5, 0.0, 0.0, 1568 0.5, 0.5, 1.0, 1.0, 1569 0.5, -0.5, 1.0, 0.0, 1570 ]); 1571 const ext = 1572 gl.getExtension("WEBGL_compressed_texture_s3tc") || 1573 gl.getExtension("MOZ_WEBGL_compressed_texture_s3tc") || 1574 gl.getExtension("WEBKIT_WEBGL_compressed_texture_s3tc") || {}; 1575 1576 let FSIZE = arr.BYTES_PER_ELEMENT; 1577 let buffer = gl.createBuffer(); // 创建缓冲区 1578 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 1579 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 1580 1581 let a_Position = gl.getAttribLocation(gl.program, 'a_Position'); // 获取attribute变量地址 1582 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 1583 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 1584 1585 let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord'); // 获取attribute变量地址 1586 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 1587 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 1588 1589 let u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler'); 1590 let texture = gl.createTexture(); 1591 1592 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 对纹理图像进行y轴反转 1593 gl.activeTexture(gl.TEXTURE0); // 开启0号纹理单元 1594 gl.bindTexture(gl.TEXTURE_2D, texture); // 向target绑定纹理对象 1595 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); // 配置纹理参数 1596 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 配置纹理参数 1597 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 配置纹理参数 1598 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 配置纹理参数 1599 const width = 4; 1600 const height = 4; 1601 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, 1602 width, height, 0, new Uint8Array([0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77,])); 1603 if (ext.COMPRESSED_RGBA_S3TC_DXT1_EXT) { 1604 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1605 } else { 1606 expect(checkError(gl)).assertEqual(gl.INVALID_ENUM); 1607 } 1608 gl.uniform1i(u_Sampler, 0); 1609 gl.clearColor(0.92, 0.92, 0.92, 1); 1610 gl.clear(gl.COLOR_BUFFER_BIT); 1611 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 1612 gl.disableVertexAttribArray(a_Position); 1613 gl.disableVertexAttribArray(a_TexCoord); 1614 gl.bindTexture(gl.TEXTURE_2D, null); 1615 gl.bindBuffer(gl.ARRAY_BUFFER, null); 1616 gl.deleteBuffer(buffer); 1617 gl.deleteTexture(texture); 1618 gl.deleteShader(vertexShader); 1619 gl.deleteShader(fragmentShader); 1620 gl.deleteProgram(program); 1621 gl.flush(); 1622 done(); 1623 }) 1624 1625 1626 /** 1627 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0041 1628 * @tc.name webgl_test_compressedTexImage2D_1 1629 * @tc.desc Test compressedTexImage2D. 1630 */ 1631 it('webgl_test_compressedTexImage2D_1', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1632 async function (done) { 1633 compressedTexImage2D((ext) => { 1634 const width = 4; 1635 const height = 4; 1636 gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, 1637 height, 0, new Uint8Array([0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77,])); 1638 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1639 }, () => { 1640 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1641 }) 1642 done() 1643 }) 1644 1645 1646 /** 1647 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0042 1648 * @tc.name webgl_test_compressedTexImage2D_2 1649 * @tc.desc Test compressedTexImage2D. 1650 */ 1651 it('webgl_test_compressedTexImage2D_2', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1652 async function (done) { 1653 compressedTexImage2D((ext) => { 1654 const width = 4; 1655 const height = 4; 1656 gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, 1657 height, 0, new Uint8Array([0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77,])); 1658 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1659 }, () => { 1660 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1661 }) 1662 done() 1663 }) 1664 1665 1666 /** 1667 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0043 1668 * @tc.name webgl_test_compressedTexImage2D_3 1669 * @tc.desc Test compressedTexImage2D. 1670 */ 1671 it('webgl_test_compressedTexImage2D_3', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1672 async function (done) { 1673 compressedTexImage2D((ext) => { 1674 const width = 4; 1675 const height = 4; 1676 gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, 1677 height, 0, new Uint8Array([0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77,])); 1678 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1679 }, () => { 1680 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1681 }) 1682 done() 1683 }) 1684 1685 1686 /** 1687 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0044 1688 * @tc.name webgl_test_compressedTexImage2D_4 1689 * @tc.desc Test compressedTexImage2D. 1690 */ 1691 it('webgl_test_compressedTexImage2D_4', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1692 async function (done) { 1693 compressedTexImage2D((ext) => { 1694 const width = 4; 1695 const height = 4; 1696 gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, 1697 height, 0, new Uint8Array([0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77,])); 1698 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1699 }, () => { 1700 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1701 }) 1702 done() 1703 }) 1704 1705 1706 /** 1707 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0045 1708 * @tc.name webgl_test_compressedTexImage2D_5 1709 * @tc.desc Test compressedTexImage2D. 1710 */ 1711 it('webgl_test_compressedTexImage2D_5', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1712 async function (done) { 1713 compressedTexImage2D((ext) => { 1714 const width = 4; 1715 const height = 4; 1716 gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, 1717 height, 0, new Uint8Array([0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77,])); 1718 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1719 }, () => { 1720 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1721 }) 1722 done() 1723 }) 1724 1725 1726 /** 1727 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0046 1728 * @tc.name webgl_test_compressedTexImage2D_6 1729 * @tc.desc Test compressedTexImage2D. 1730 */ 1731 it('webgl_test_compressedTexImage2D_6', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1732 async function (done) { 1733 compressedTexImage2D((ext) => { 1734 const width = 4; 1735 const height = 4; 1736 gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, 1737 height, 0, new Uint8Array([0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77,])); 1738 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1739 }, () => { 1740 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1741 }) 1742 done() 1743 }) 1744 1745 1746 /** 1747 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0047 1748 * @tc.name webgl_test_compressedTexImage2D_Error 1749 * @tc.desc Test compressedTexImage2D. 1750 */ 1751 it('webgl_test_compressedTexImage2D_Error', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1752 async function (done) { 1753 console.info("webgltest [webgl_test_compressedTexImage2D_Error] compressedTexImage2D"); 1754 const width = 500; 1755 const height = 500; 1756 const ext = 1757 gl.getExtension("WEBGL_compressed_texture_s3tc") || 1758 gl.getExtension("MOZ_WEBGL_compressed_texture_s3tc") || 1759 gl.getExtension("WEBKIT_WEBGL_compressed_texture_s3tc") || {}; 1760 console.info("webgltest [webgl_test_compressedTexImage2D_Error] compressedTexImage2D no target"); 1761 gl.compressedTexImage2D(undefined, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0, new Uint8Array([ 1762 0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77, 1763 ])); 1764 expect(checkError(gl)).assertEqual(gl.INVALID_ENUM); 1765 console.info("webgltest [webgl_test_compressedTexImage2D_Error] compressedTexImage2D no level"); 1766 gl.compressedTexImage2D(gl.TEXTURE_2D, undefined, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0, new Uint8Array([ 1767 0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77, 1768 ])); 1769 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1770 console.info("webgltest [webgl_test_compressedTexImage2D_Error] compressedTexImage2D no internalformat"); 1771 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, undefined, width, height, 0, new Uint8Array([ 1772 0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77, 1773 ])); 1774 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1775 console.info("webgltest [webgl_test_compressedTexImage2D_Error] compressedTexImage2D no width"); 1776 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, undefined, height, 0, new Uint8Array([ 1777 0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77, 1778 ])); 1779 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1780 console.info("webgltest [webgl_test_compressedTexImage2D_Error] compressedTexImage2D no height"); 1781 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, undefined, 0, new Uint8Array([ 1782 0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77, 1783 ])); 1784 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1785 console.info("webgltest [webgl_test_compressedTexImage2D_Error] compressedTexImage2D no border"); 1786 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, undefined, new Uint8Array([ 1787 0x1e, 0x33, 0xaa, 0xaF, 0x1e, 0x88, 0x1e, 0x77, 1788 ])); 1789 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1790 console.info("webgltest [webgl_test_compressedTexImage2D_Error] compressedTexImage2D no pixels"); 1791 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0, new Uint8Array([])); 1792 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1793 done() 1794 }) 1795 1796 1797 /** 1798 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0048 1799 * @tc.name webgl_test_compressedTexSubImage2D 1800 * @tc.desc Test compressedTexSubImage2D. 1801 */ 1802 it('webgl_test_compressedTexSubImage2D', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1803 async function (done) { 1804 console.info("webgltest [webgl_test_compressedTexSubImage2D] compressedTexSubImage2D"); 1805 let availableExtensions = gl.getSupportedExtensions(); 1806 for (var i = 0; i < availableExtensions.length; i++) { 1807 if (availableExtensions[i].indexOf('texture') >= 0 1808 && availableExtensions[i].indexOf('compressed') >= 0) { 1809 console.log("webgltest extension:", availableExtensions[i]); 1810 } 1811 } 1812 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 1813 attribute vec4 a_Position; 1814 attribute vec2 a_TexCoord; 1815 varying vec2 v_TexCoord; 1816 void main(){ 1817 gl_Position = a_Position; 1818 v_TexCoord = a_TexCoord; 1819 } 1820 `, ` 1821 precision mediump float; 1822 precision highp sampler2D; 1823 uniform sampler2D u_Sampler; 1824 varying vec2 v_TexCoord; 1825 void main(){ 1826 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 1827 } 1828 `); 1829 let arr = new Float32Array([ 1830 -0.5, 0.5, 0.0, 1.0, 1831 -0.5, -0.5, 0.0, 0.0, 1832 0.5, 0.5, 1.0, 1.0, 1833 0.5, -0.5, 1.0, 0.0, 1834 ]); 1835 const ext = 1836 gl.getExtension("WEBGL_compressed_texture_s3tc") || 1837 gl.getExtension("MOZ_WEBGL_compressed_texture_s3tc") || 1838 gl.getExtension("WEBKIT_WEBGL_compressed_texture_s3tc") || {}; 1839 1840 let FSIZE = arr.BYTES_PER_ELEMENT; 1841 let buffer = gl.createBuffer(); // 创建缓冲区 1842 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 1843 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 1844 1845 let a_Position = gl.getAttribLocation(gl.program, 'a_Position'); // 获取attribute变量地址 1846 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 1847 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 1848 1849 let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord'); // 获取attribute变量地址 1850 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 1851 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 1852 1853 let u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler'); 1854 let texture = gl.createTexture(); 1855 1856 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 对纹理图像进行y轴反转 1857 gl.activeTexture(gl.TEXTURE0); // 开启0号纹理单元 1858 gl.bindTexture(gl.TEXTURE_2D, texture); // 向target绑定纹理对象 1859 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); // 配置纹理参数 1860 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // 配置纹理参数 1861 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // 配置纹理参数 1862 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // 配置纹理参数 1863 const width = 4; 1864 const height = 4; 1865 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0, new Uint8Array([ 1866 0x1e, 0x33, 0xaa, 0xaF, 1867 0x1e, 0x88, 0x1e, 0x77, 1868 ])); 1869 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, new Uint8Array([ 1870 0xe0, 0x07, 0x00, 0xf8, 1871 0x13, 0x10, 0x15, 0x00, 1872 ])) 1873 if (ext.COMPRESSED_RGBA_S3TC_DXT1_EXT) { 1874 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1875 } else { 1876 expect(checkError(gl)).assertEqual(gl.INVALID_ENUM); 1877 } 1878 gl.uniform1i(u_Sampler, 0); 1879 gl.clearColor(0.92, 0.92, 0.92, 1); 1880 gl.clear(gl.COLOR_BUFFER_BIT); 1881 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 1882 gl.disableVertexAttribArray(a_Position); 1883 gl.disableVertexAttribArray(a_TexCoord); 1884 gl.bindTexture(gl.TEXTURE_2D, null); 1885 gl.bindBuffer(gl.ARRAY_BUFFER, null); 1886 gl.deleteBuffer(buffer); 1887 gl.deleteTexture(texture); 1888 gl.deleteShader(vertexShader); 1889 gl.deleteShader(fragmentShader); 1890 gl.deleteProgram(program); 1891 gl.flush(); 1892 checkError(gl); 1893 done(); 1894 }) 1895 1896 /** 1897 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0049 1898 * @tc.name webgl_test_compressedTexSubImage2D_Error 1899 * @tc.desc Test compressedTexSubImage2D. 1900 */ 1901 it('webgl_test_compressedTexSubImage2D_Error', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 1902 async function (done) { 1903 console.info("webgltest [webgl_test_compressedTexSubImage2D_Error] compressedTexSubImage2D"); 1904 const width = 500; 1905 const height = 500; 1906 const ext = 1907 gl.getExtension("WEBGL_compressed_texture_s3tc") || 1908 gl.getExtension("MOZ_WEBGL_compressed_texture_s3tc") || 1909 gl.getExtension("WEBKIT_WEBGL_compressed_texture_s3tc") || {}; 1910 console.info("webgltest [webgl_test_compressedTexSubImage2D] compressedTexSubImage2D no target"); 1911 gl.compressedTexSubImage2D(undefined, 0, 0, 0, width, height, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, new Uint8Array([ 1912 0xe0, 0x07, 0x00, 0xf8, 1913 0x13, 0x10, 0x15, 0x00, 1914 ])) 1915 expect(checkError(gl)).assertEqual(gl.INVALID_ENUM); 1916 console.info("webgltest [webgl_test_compressedTexSubImage2D] compressedTexSubImage2D no level"); 1917 gl.compressedTexSubImage2D(gl.TEXTURE_2D, undefined, 0, 0, width, height, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, new Uint8Array([ 1918 0xe0, 0x07, 0x00, 0xf8, 1919 0x13, 0x10, 0x15, 0x00, 1920 ])) 1921 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1922 console.info("webgltest [webgl_test_compressedTexSubImage2D] compressedTexSubImage2D no xoffset"); 1923 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, undefined, 0, width, height, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, new Uint8Array([ 1924 0xe0, 0x07, 0x00, 0xf8, 1925 0x13, 0x10, 0x15, 0x00, 1926 ])) 1927 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1928 console.info("webgltest [webgl_test_compressedTexSubImage2D] compressedTexSubImage2D no yoffset"); 1929 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, undefined, width, height, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, new Uint8Array([ 1930 0xe0, 0x07, 0x00, 0xf8, 1931 0x13, 0x10, 0x15, 0x00, 1932 ])) 1933 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1934 console.info("webgltest [webgl_test_compressedTexSubImage2D] compressedTexSubImage2D no width"); 1935 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, undefined, height, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, new Uint8Array([ 1936 0xe0, 0x07, 0x00, 0xf8, 1937 0x13, 0x10, 0x15, 0x00, 1938 ])) 1939 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1940 console.info("webgltest [webgl_test_compressedTexSubImage2D] compressedTexSubImage2D no height"); 1941 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, undefined, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, new Uint8Array([ 1942 0xe0, 0x07, 0x00, 0xf8, 1943 0x13, 0x10, 0x15, 0x00, 1944 ])) 1945 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1946 console.info("webgltest [webgl_test_compressedTexSubImage2D] compressedTexSubImage2D no format"); 1947 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, undefined, new Uint8Array([ 1948 0xe0, 0x07, 0x00, 0xf8, 1949 0x13, 0x10, 0x15, 0x00, 1950 ])) 1951 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1952 console.info("webgltest [webgl_test_compressedTexSubImage2D] compressedTexSubImage2D no srcData"); 1953 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, ext.COMPRESSED_RGBA_S3TC_DXT1_EXT, new Uint8Array([])) 1954 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 1955 done() 1956 }) 1957 1958 /** 1959 * 方法将像素从当前 WebGLFramebuffer 复制到 2D 纹理图像中。 1960 */ 1961 1962 function copyTexImage2D(callback, finish) { 1963 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D"); 1964 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 1965 void main(){ 1966 gl_Position = vec4(0.0, 0.0, 0.0, 1.0); 1967 gl_PointSize = 100.0; 1968 } 1969 `, ` 1970 precision mediump float; 1971 void main(){ 1972 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 1973 } 1974 `); 1975 let srcViewPort = gl.getParameter(gl.VIEWPORT); 1976 console.log(srcViewPort); 1977 gl.useProgram(program); 1978 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); 1979 gl.activeTexture(gl.TEXTURE0); 1980 let texture = gl.createTexture(); 1981 gl.bindTexture(gl.TEXTURE_2D, texture); 1982 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 1983 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 1984 let frameBuffer = gl.createFramebuffer(); 1985 gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer); 1986 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); 1987 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 1988 gl.viewport(0, 0, 256, 256); 1989 gl.clearColor(0.9, 0.9, 0.9, 1.0); 1990 gl.clear(gl.COLOR_BUFFER_BIT); 1991 gl.drawArrays(gl.POINTS, 0, 1); 1992 let newTexture = gl.createTexture(); 1993 gl.bindTexture(gl.TEXTURE_2D, newTexture); 1994 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 1995 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 1996 callback() 1997 finish() 1998 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 1999 gl.viewport(srcViewPort[0], srcViewPort[1], srcViewPort[2], srcViewPort[3]); 2000 let p2 = createProgram(gl, ` 2001 attribute vec4 a_Position; 2002 attribute vec2 a_TexCoord; 2003 varying vec2 v_TexCoord; 2004 void main(){ 2005 gl_Position = a_Position; 2006 v_TexCoord = a_TexCoord; 2007 } 2008 `, ` 2009 precision mediump float; 2010 uniform sampler2D u_Sampler; 2011 varying vec2 v_TexCoord; 2012 void main(){ 2013 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 2014 } 2015 `); 2016 2017 gl.useProgram(p2.program); 2018 let arr = new Float32Array([ 2019 -0.5, 0.5, 0.0, 1.0, 2020 -0.5, -0.5, 0.0, 0.0, 2021 0.5, 0.5, 1.0, 1.0, 2022 0.5, -0.5, 1.0, 0.0, 2023 ]); 2024 let FSIZE = arr.BYTES_PER_ELEMENT; 2025 let buffer = gl.createBuffer(); // 创建缓冲区 2026 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 2027 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 2028 let a_Position = gl.getAttribLocation(p2.program, 'a_Position'); // 获取attribute变量地址 2029 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 2030 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 2031 let a_TexCoord = gl.getAttribLocation(p2.program, 'a_TexCoord'); // 获取attribute变量地址 2032 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 2033 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 2034 2035 gl.bindTexture(gl.TEXTURE_2D, newTexture); 2036 let u_Sampler = gl.getUniformLocation(p2.program, 'u_Sampler'); 2037 gl.uniform1i(u_Sampler, 0); 2038 gl.clearColor(1.92, 0.92, 0.92, 1); 2039 gl.clear(gl.COLOR_BUFFER_BIT); 2040 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 2041 gl.flush(); 2042 gl.disableVertexAttribArray(a_Position); 2043 gl.disableVertexAttribArray(a_TexCoord); 2044 gl.bindTexture(gl.TEXTURE_2D, null); 2045 gl.bindBuffer(gl.ARRAY_BUFFER, null); 2046 gl.deleteBuffer(buffer); 2047 gl.deleteFramebuffer(frameBuffer); 2048 gl.deleteTexture(texture); 2049 gl.deleteTexture(newTexture); 2050 gl.deleteShader(vertexShader); 2051 gl.deleteShader(fragmentShader); 2052 gl.deleteProgram(program); 2053 gl.deleteShader(p2.vertexShader); 2054 gl.deleteShader(p2.fragmentShader); 2055 gl.deleteProgram(p2.program); 2056 } 2057 2058 2059 /** 2060 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0050 2061 * @tc.name webgl_test_copyTexImage2D 2062 * @tc.desc Test copyTexImage2D. 2063 */ 2064 it('webgl_test_copyTexImage2D', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 2065 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D"); 2066 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 2067 void main(){ 2068 gl_Position = vec4(0.0, 0.0, 0.0, 1.0); 2069 gl_PointSize = 100.0; 2070 } 2071 `, ` 2072 precision mediump float; 2073 void main(){ 2074 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 2075 } 2076 `); 2077 let srcViewPort = gl.getParameter(gl.VIEWPORT); 2078 console.log(srcViewPort); 2079 gl.useProgram(program); 2080 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); 2081 gl.activeTexture(gl.TEXTURE0); 2082 let texture = gl.createTexture(); 2083 gl.bindTexture(gl.TEXTURE_2D, texture); 2084 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 2085 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 2086 let frameBuffer = gl.createFramebuffer(); 2087 gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer); 2088 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); 2089 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 2090 gl.viewport(0, 0, 256, 256); 2091 gl.clearColor(0.9, 0.9, 0.9, 1.0); 2092 gl.clear(gl.COLOR_BUFFER_BIT); 2093 gl.drawArrays(gl.POINTS, 0, 1); 2094 let newTexture = gl.createTexture(); 2095 gl.bindTexture(gl.TEXTURE_2D, newTexture); 2096 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 2097 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 2098 gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 128, 128, 128, 128, 0); 2099 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 2100 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 2101 gl.viewport(srcViewPort[0], srcViewPort[1], srcViewPort[2], srcViewPort[3]); 2102 let p2 = createProgram(gl, ` 2103 attribute vec4 a_Position; 2104 attribute vec2 a_TexCoord; 2105 varying vec2 v_TexCoord; 2106 void main(){ 2107 gl_Position = a_Position; 2108 v_TexCoord = a_TexCoord; 2109 } 2110 `, ` 2111 precision mediump float; 2112 uniform sampler2D u_Sampler; 2113 varying vec2 v_TexCoord; 2114 void main(){ 2115 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 2116 } 2117 `); 2118 2119 gl.useProgram(p2.program); 2120 let arr = new Float32Array([ 2121 -0.5, 0.5, 0.0, 1.0, 2122 -0.5, -0.5, 0.0, 0.0, 2123 0.5, 0.5, 1.0, 1.0, 2124 0.5, -0.5, 1.0, 0.0, 2125 ]); 2126 let FSIZE = arr.BYTES_PER_ELEMENT; 2127 let buffer = gl.createBuffer(); 2128 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); 2129 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); 2130 let a_Position = gl.getAttribLocation(p2.program, 'a_Position'); 2131 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); 2132 gl.enableVertexAttribArray(a_Position); 2133 let a_TexCoord = gl.getAttribLocation(p2.program, 'a_TexCoord'); 2134 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); 2135 gl.enableVertexAttribArray(a_TexCoord); 2136 2137 gl.bindTexture(gl.TEXTURE_2D, newTexture); 2138 let u_Sampler = gl.getUniformLocation(p2.program, 'u_Sampler'); 2139 gl.uniform1i(u_Sampler, 0); 2140 gl.clearColor(1.92, 0.92, 0.92, 1); 2141 gl.clear(gl.COLOR_BUFFER_BIT); 2142 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 2143 gl.flush(); 2144 gl.disableVertexAttribArray(a_Position); 2145 gl.disableVertexAttribArray(a_TexCoord); 2146 gl.bindTexture(gl.TEXTURE_2D, null); 2147 gl.bindBuffer(gl.ARRAY_BUFFER, null); 2148 gl.deleteBuffer(buffer); 2149 gl.deleteFramebuffer(frameBuffer); 2150 gl.deleteTexture(texture); 2151 gl.deleteTexture(newTexture); 2152 gl.deleteShader(vertexShader); 2153 gl.deleteShader(fragmentShader); 2154 gl.deleteProgram(program); 2155 gl.deleteShader(p2.vertexShader); 2156 gl.deleteShader(p2.fragmentShader); 2157 gl.deleteProgram(p2.program); 2158 done(); 2159 }) 2160 2161 2162 /** 2163 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0051 2164 * @tc.name webgl_test_copyTexImage2D_1 2165 * @tc.desc Test copyTexImage2D. 2166 */ 2167 it('webgl_test_copyTexImage2D_1', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 2168 copyTexImage2D(() => { 2169 gl.copyTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X, 0, gl.RGBA, 128, 128, 128, 128, 0); 2170 }, () => { 2171 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2172 }) 2173 done() 2174 }) 2175 2176 2177 /** 2178 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0052 2179 * @tc.name webgl_test_copyTexImage2D_2 2180 * @tc.desc Test copyTexImage2D. 2181 */ 2182 it('webgl_test_copyTexImage2D_2', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 2183 copyTexImage2D(() => { 2184 gl.copyTexImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 0, gl.RGBA, 128, 128, 128, 128, 0); 2185 }, () => { 2186 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2187 }) 2188 done() 2189 }) 2190 2191 2192 /** 2193 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0053 2194 * @tc.name webgl_test_copyTexImage2D_3 2195 * @tc.desc Test copyTexImage2D. 2196 */ 2197 it('webgl_test_copyTexImage2D_3', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 2198 copyTexImage2D(() => { 2199 gl.copyTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 0, gl.RGBA, 128, 128, 128, 128, 0); 2200 }, () => { 2201 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2202 }) 2203 done() 2204 }) 2205 2206 2207 /** 2208 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0054 2209 * @tc.name webgl_test_copyTexImage2D_4 2210 * @tc.desc Test copyTexImage2D. 2211 */ 2212 it('webgl_test_copyTexImage2D_4', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 2213 copyTexImage2D(() => { 2214 gl.copyTexImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, gl.RGBA, 128, 128, 128, 128, 0); 2215 }, () => { 2216 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2217 }) 2218 done() 2219 }) 2220 2221 2222 /** 2223 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0055 2224 * @tc.name webgl_test_copyTexImage2D_5 2225 * @tc.desc Test copyTexImage2D. 2226 */ 2227 it('webgl_test_copyTexImage2D_5', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 2228 copyTexImage2D(() => { 2229 gl.copyTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 0, gl.RGBA, 128, 128, 128, 128, 0); 2230 }, () => { 2231 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2232 }) 2233 done() 2234 }) 2235 2236 2237 /** 2238 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0056 2239 * @tc.name webgl_test_copyTexImage2D_6 2240 * @tc.desc Test copyTexImage2D. 2241 */ 2242 it('webgl_test_copyTexImage2D_6', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 2243 copyTexImage2D(() => { 2244 gl.copyTexImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, gl.RGBA, 128, 128, 128, 128, 0); 2245 }, () => { 2246 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2247 }) 2248 done() 2249 }) 2250 2251 2252 /** 2253 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0057 2254 * @tc.name webgl_test_copyTexImage2D_Error 2255 * @tc.desc Test copyTexImage2D. 2256 */ 2257 it('webgl_test_copyTexImage2D_Error', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2258 async function (done) { 2259 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D"); 2260 const width = 500; 2261 const height = 500; 2262 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D no target"); 2263 gl.copyTexImage2D(undefined, 0, 0, 0, 0, width, height, 0) 2264 checkError(gl) 2265 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D no level"); 2266 gl.copyTexImage2D(gl.TEXTURE_2D, undefined, 0, 0, 0, width, height, 0) 2267 checkError(gl) 2268 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D no internalformat"); 2269 gl.copyTexImage2D(gl.TEXTURE_2D, 0, undefined, 0, 0, width, height, 0) 2270 checkError(gl) 2271 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D no x"); 2272 gl.copyTexImage2D(gl.TEXTURE_2D, 0, 0, undefined, 0, width, height, 0) 2273 checkError(gl) 2274 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D no y"); 2275 gl.copyTexImage2D(gl.TEXTURE_2D, 0, 0, 0, undefined, width, height, 0) 2276 checkError(gl) 2277 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D no width"); 2278 gl.copyTexImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, undefined, height, 0) 2279 checkError(gl) 2280 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D no height"); 2281 gl.copyTexImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, width, undefined, 0) 2282 checkError(gl) 2283 console.info("webgltest [webgl_test_copyTexImage2D] copyTexImage2D no border"); 2284 gl.copyTexImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, width, height, undefined) 2285 checkError(gl) 2286 done() 2287 }) 2288 2289 /** 2290 * 方法将像素从当前 WebGLFramebuffer 复制到现有的 2D 纹理子图像中。 2291 */ 2292 function copyTexSubImage2D(callback, finish) { 2293 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D"); 2294 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 2295 void main(){ 2296 gl_Position = vec4(0.0, 0.0, 0.0, 1.0); 2297 gl_PointSize = 50.0; 2298 } 2299 `, ` 2300 precision mediump float; 2301 void main(){ 2302 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 2303 } 2304 `); 2305 let srcViewPort = gl.getParameter(gl.VIEWPORT); 2306 console.log(srcViewPort); 2307 gl.useProgram(program); 2308 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); 2309 gl.activeTexture(gl.TEXTURE0); 2310 let texture = gl.createTexture(); 2311 gl.bindTexture(gl.TEXTURE_2D, texture); 2312 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 2313 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 2314 let frameBuffer = gl.createFramebuffer(); 2315 gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer); 2316 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); 2317 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 2318 gl.viewport(0, 0, 256, 256); 2319 gl.clearColor(0.9, 0.9, 0.9, 1.0); 2320 gl.clear(gl.COLOR_BUFFER_BIT); 2321 gl.drawArrays(gl.POINTS, 0, 1); 2322 let newTexture = gl.createTexture(); 2323 gl.bindTexture(gl.TEXTURE_2D, newTexture); 2324 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 2325 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 2326 console.info("webgltest", gl.isTexture(texture), gl.isTexture(newTexture)); 2327 callback() 2328 finish() 2329 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 2330 gl.viewport(srcViewPort[0], srcViewPort[1], srcViewPort[2], srcViewPort[3]); 2331 let p2 = createProgram(gl, ` 2332 attribute vec4 a_Position; 2333 attribute vec2 a_TexCoord; 2334 varying vec2 v_TexCoord; 2335 void main(){ 2336 gl_Position = a_Position; 2337 v_TexCoord = a_TexCoord; 2338 } 2339 `, ` 2340 precision mediump float; 2341 uniform sampler2D u_Sampler; 2342 varying vec2 v_TexCoord; 2343 void main(){ 2344 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 2345 } 2346 `); 2347 2348 gl.useProgram(p2.program); 2349 let arr = new Float32Array([ 2350 -0.5, 0.5, 0.0, 1.0, 2351 -0.5, -0.5, 0.0, 0.0, 2352 0.5, 0.5, 1.0, 1.0, 2353 0.5, -0.5, 1.0, 0.0, 2354 ]); 2355 let FSIZE = arr.BYTES_PER_ELEMENT; 2356 let buffer = gl.createBuffer(); 2357 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); 2358 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); 2359 let a_Position = gl.getAttribLocation(p2.program, 'a_Position'); 2360 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); 2361 gl.enableVertexAttribArray(a_Position); 2362 let a_TexCoord = gl.getAttribLocation(p2.program, 'a_TexCoord'); 2363 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); 2364 gl.enableVertexAttribArray(a_TexCoord); 2365 2366 gl.bindTexture(gl.TEXTURE_2D, newTexture); 2367 let u_Sampler = gl.getUniformLocation(p2.program, 'u_Sampler'); 2368 gl.uniform1i(u_Sampler, 0); 2369 gl.clearColor(1.92, 0.92, 0.92, 1); 2370 gl.clear(gl.COLOR_BUFFER_BIT); 2371 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 2372 gl.flush(); 2373 gl.disableVertexAttribArray(a_Position); 2374 gl.disableVertexAttribArray(a_TexCoord); 2375 gl.bindTexture(gl.TEXTURE_2D, null); 2376 gl.bindBuffer(gl.ARRAY_BUFFER, null); 2377 gl.deleteBuffer(buffer); 2378 gl.deleteFramebuffer(frameBuffer); 2379 gl.deleteTexture(texture); 2380 gl.deleteTexture(newTexture); 2381 gl.deleteShader(vertexShader); 2382 gl.deleteShader(fragmentShader); 2383 gl.deleteProgram(program); 2384 gl.deleteShader(p2.vertexShader); 2385 gl.deleteShader(p2.fragmentShader); 2386 gl.deleteProgram(p2.program); 2387 } 2388 2389 2390 /** 2391 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0058 2392 * @tc.name webgl_test_copyTexSubImage2D 2393 * @tc.desc Test copyTexSubImage2D. 2394 */ 2395 it('webgl_test_copyTexSubImage2D', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 2396 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D"); 2397 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 2398 void main(){ 2399 gl_Position = vec4(0.0, 0.0, 0.0, 1.0); 2400 gl_PointSize = 50.0; 2401 } 2402 `, ` 2403 precision mediump float; 2404 void main(){ 2405 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 2406 } 2407 `); 2408 let srcViewPort = gl.getParameter(gl.VIEWPORT); 2409 console.log(srcViewPort); 2410 gl.useProgram(program); 2411 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); 2412 gl.activeTexture(gl.TEXTURE0); 2413 let texture = gl.createTexture(); 2414 gl.bindTexture(gl.TEXTURE_2D, texture); 2415 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 2416 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 2417 let frameBuffer = gl.createFramebuffer(); 2418 gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer); 2419 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); 2420 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 2421 gl.viewport(0, 0, 256, 256); 2422 gl.clearColor(0.9, 0.9, 0.9, 1.0); 2423 gl.clear(gl.COLOR_BUFFER_BIT); 2424 gl.drawArrays(gl.POINTS, 0, 1); 2425 let newTexture = gl.createTexture(); 2426 gl.bindTexture(gl.TEXTURE_2D, newTexture); 2427 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 2428 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 2429 console.info("webgltest", gl.isTexture(texture), gl.isTexture(newTexture)); 2430 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 32, 32, 32, 64, 128, 128); 2431 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 2432 2433 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 2434 gl.viewport(srcViewPort[0], srcViewPort[1], srcViewPort[2], srcViewPort[3]); 2435 let p2 = createProgram(gl, ` 2436 attribute vec4 a_Position; 2437 attribute vec2 a_TexCoord; 2438 varying vec2 v_TexCoord; 2439 void main(){ 2440 gl_Position = a_Position; 2441 v_TexCoord = a_TexCoord; 2442 } 2443 `, ` 2444 precision mediump float; 2445 uniform sampler2D u_Sampler; 2446 varying vec2 v_TexCoord; 2447 void main(){ 2448 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 2449 } 2450 `); 2451 2452 gl.useProgram(p2.program); 2453 let arr = new Float32Array([ 2454 -0.5, 0.5, 0.0, 1.0, 2455 -0.5, -0.5, 0.0, 0.0, 2456 0.5, 0.5, 1.0, 1.0, 2457 0.5, -0.5, 1.0, 0.0, 2458 ]); 2459 let FSIZE = arr.BYTES_PER_ELEMENT; 2460 let buffer = gl.createBuffer(); 2461 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); 2462 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); 2463 let a_Position = gl.getAttribLocation(p2.program, 'a_Position'); 2464 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); 2465 gl.enableVertexAttribArray(a_Position); 2466 let a_TexCoord = gl.getAttribLocation(p2.program, 'a_TexCoord'); 2467 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); 2468 gl.enableVertexAttribArray(a_TexCoord); 2469 2470 gl.bindTexture(gl.TEXTURE_2D, newTexture); 2471 let u_Sampler = gl.getUniformLocation(p2.program, 'u_Sampler'); 2472 gl.uniform1i(u_Sampler, 0); 2473 gl.clearColor(1.92, 0.92, 0.92, 1); 2474 gl.clear(gl.COLOR_BUFFER_BIT); 2475 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 2476 gl.flush(); 2477 gl.disableVertexAttribArray(a_Position); 2478 gl.disableVertexAttribArray(a_TexCoord); 2479 gl.bindTexture(gl.TEXTURE_2D, null); 2480 gl.bindBuffer(gl.ARRAY_BUFFER, null); 2481 gl.deleteBuffer(buffer); 2482 gl.deleteFramebuffer(frameBuffer); 2483 gl.deleteTexture(texture); 2484 gl.deleteTexture(newTexture); 2485 gl.deleteShader(vertexShader); 2486 gl.deleteShader(fragmentShader); 2487 gl.deleteProgram(program); 2488 gl.deleteShader(p2.vertexShader); 2489 gl.deleteShader(p2.fragmentShader); 2490 gl.deleteProgram(p2.program); 2491 done(); 2492 }) 2493 2494 2495 /** 2496 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0059 2497 * @tc.name webgl_test_copyTexSubImage2D_1 2498 * @tc.desc Test copyTexSubImage2D. 2499 */ 2500 it('webgl_test_copyTexSubImage2D_1', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2501 async function (done) { 2502 copyTexSubImage2D(() => { 2503 gl.copyTexSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X, 0, 32, 32, 32, 64, 128, 128); 2504 }, () => { 2505 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2506 }) 2507 done() 2508 }) 2509 2510 2511 /** 2512 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0060 2513 * @tc.name webgl_test_copyTexSubImage2D_2 2514 * @tc.desc Test copyTexSubImage2D. 2515 */ 2516 it('webgl_test_copyTexSubImage2D_2', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2517 async function (done) { 2518 copyTexSubImage2D(() => { 2519 gl.copyTexSubImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 0, 32, 32, 32, 64, 128, 128); 2520 }, () => { 2521 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2522 }) 2523 done() 2524 }) 2525 2526 2527 /** 2528 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0061 2529 * @tc.name webgl_test_copyTexSubImage2D_3 2530 * @tc.desc Test copyTexSubImage2D. 2531 */ 2532 it('webgl_test_copyTexSubImage2D_3', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2533 async function (done) { 2534 copyTexSubImage2D(() => { 2535 gl.copyTexSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 0, 32, 32, 32, 64, 128, 128); 2536 }, () => { 2537 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2538 }) 2539 done() 2540 }) 2541 2542 2543 /** 2544 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0062 2545 * @tc.name webgl_test_copyTexSubImage2D_4 2546 * @tc.desc Test copyTexSubImage2D. 2547 */ 2548 it('webgl_test_copyTexSubImage2D_4', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2549 async function (done) { 2550 copyTexSubImage2D(() => { 2551 gl.copyTexSubImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, 32, 32, 32, 64, 128, 128); 2552 }, () => { 2553 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2554 }) 2555 done() 2556 }) 2557 2558 2559 /** 2560 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0063 2561 * @tc.name webgl_test_copyTexSubImage2D_5 2562 * @tc.desc Test copyTexSubImage2D. 2563 */ 2564 it('webgl_test_copyTexSubImage2D_5', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2565 async function (done) { 2566 copyTexSubImage2D(() => { 2567 gl.copyTexSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 0, 32, 32, 32, 64, 128, 128); 2568 }, () => { 2569 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2570 }) 2571 done() 2572 }) 2573 2574 2575 /** 2576 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0064 2577 * @tc.name webgl_test_copyTexSubImage2D_6 2578 * @tc.desc Test copyTexSubImage2D. 2579 */ 2580 it('webgl_test_copyTexSubImage2D_6', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2581 async function (done) { 2582 copyTexSubImage2D(() => { 2583 gl.copyTexSubImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, 32, 32, 32, 64, 128, 128); 2584 }, () => { 2585 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2586 }) 2587 done() 2588 }) 2589 2590 2591 /** 2592 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0065 2593 * @tc.name webgl_test_copyTexSubImage2D_Error 2594 * @tc.desc Test copyTexSubImage2D. 2595 */ 2596 it('webgl_test_copyTexSubImage2D_Error', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2597 async function (done) { 2598 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D"); 2599 const width = 500; 2600 const height = 500; 2601 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D no target"); 2602 gl.copyTexSubImage2D(undefined, 0, 0, 0, 0, 0, width, height) 2603 checkError(gl) 2604 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D no level"); 2605 gl.copyTexSubImage2D(gl.TEXTURE_2D, undefined, 0, 0, 0, 0, width, height) 2606 checkError(gl) 2607 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexImage2D no xoffset"); 2608 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, undefined, 0, 0, 0, width, height) 2609 checkError(gl) 2610 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D no yoffset"); 2611 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, undefined, 0, 0, width, height) 2612 checkError(gl) 2613 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D no x"); 2614 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, undefined, 0, width, height) 2615 checkError(gl) 2616 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D no y"); 2617 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, undefined, width, height) 2618 checkError(gl) 2619 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D no width"); 2620 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, undefined, height) 2621 checkError(gl) 2622 console.info("webgltest [webgl_test_copyTexSubImage2D] copyTexSubImage2D no height"); 2623 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, width, undefined) 2624 checkError(gl) 2625 done() 2626 }) 2627 2628 function framebufferTexture2D(callback, finish) { 2629 console.info("webgltest [webgl_test_framebufferTexture2D] framebufferTexture2D"); 2630 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 2631 void main(){ 2632 gl_Position = vec4(0.0, 0.0, 0.0, 1.0); 2633 gl_PointSize = 100.0; 2634 } 2635 `, ` 2636 precision mediump float; 2637 void main(){ 2638 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 2639 } 2640 `); 2641 let srcViewPort = gl.getParameter(gl.VIEWPORT); 2642 console.log(srcViewPort); 2643 gl.useProgram(program); 2644 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); 2645 gl.activeTexture(gl.TEXTURE0); 2646 let texture = gl.createTexture(); 2647 gl.bindTexture(gl.TEXTURE_2D, texture); 2648 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 2649 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 2650 let frameBuffer = gl.createFramebuffer(); 2651 gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer); 2652 callback(texture) 2653 gl.viewport(0, 0, 256, 256); 2654 gl.clearColor(0.9, 0.9, 0.9, 1.0); 2655 gl.clear(gl.COLOR_BUFFER_BIT); 2656 gl.drawArrays(gl.POINTS, 0, 1); 2657 2658 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 2659 gl.viewport(srcViewPort[0], srcViewPort[1], srcViewPort[2], srcViewPort[3]); 2660 let p2 = createProgram(gl, ` 2661 attribute vec4 a_Position; 2662 attribute vec2 a_TexCoord; 2663 varying vec2 v_TexCoord; 2664 void main(){ 2665 gl_Position = a_Position; 2666 v_TexCoord = a_TexCoord; 2667 } 2668 `, ` 2669 precision mediump float; 2670 uniform sampler2D u_Sampler; 2671 varying vec2 v_TexCoord; 2672 void main(){ 2673 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 2674 } 2675 `); 2676 2677 gl.useProgram(p2.program); 2678 let arr = new Float32Array([ 2679 -0.5, 0.5, 0.0, 1.0, 2680 -0.5, -0.5, 0.0, 0.0, 2681 0.5, 0.5, 1.0, 1.0, 2682 0.5, -0.5, 1.0, 0.0, 2683 ]); 2684 let FSIZE = arr.BYTES_PER_ELEMENT; 2685 let buffer = gl.createBuffer(); 2686 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); 2687 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); 2688 let a_Position = gl.getAttribLocation(p2.program, 'a_Position'); 2689 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); 2690 gl.enableVertexAttribArray(a_Position); 2691 let a_TexCoord = gl.getAttribLocation(p2.program, 'a_TexCoord'); 2692 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); 2693 gl.enableVertexAttribArray(a_TexCoord); 2694 gl.bindTexture(gl.TEXTURE_2D, texture); 2695 let u_Sampler = gl.getUniformLocation(p2.program, 'u_Sampler'); 2696 gl.uniform1i(u_Sampler, 0); 2697 gl.clearColor(1.92, 0.92, 0.92, 1); 2698 gl.clear(gl.COLOR_BUFFER_BIT); 2699 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 2700 gl.flush(); 2701 gl.disableVertexAttribArray(a_Position); 2702 gl.disableVertexAttribArray(a_TexCoord); 2703 gl.bindTexture(gl.TEXTURE_2D, null); 2704 gl.bindBuffer(gl.ARRAY_BUFFER, null); 2705 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 2706 gl.deleteBuffer(buffer); 2707 gl.deleteFramebuffer(frameBuffer); 2708 gl.deleteTexture(texture); 2709 gl.deleteShader(vertexShader); 2710 gl.deleteShader(fragmentShader); 2711 gl.deleteProgram(program); 2712 gl.deleteShader(p2.vertexShader); 2713 gl.deleteShader(p2.fragmentShader); 2714 gl.deleteProgram(p2.program); 2715 finish() 2716 } 2717 2718 2719 /** 2720 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0066 2721 * @tc.name webgl_test_framebufferTexture2D 2722 * @tc.desc Test framebufferTexture2D. 2723 */ 2724 it('webgl_test_framebufferTexture2D', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2725 async function (done) { 2726 console.info("webgltest [webgl_test_framebufferTexture2D] framebufferTexture2D"); 2727 let {program, vertexShader, fragmentShader} = createProgram(gl, ` 2728 void main(){ 2729 gl_Position = vec4(0.0, 0.0, 0.0, 1.0); 2730 gl_PointSize = 100.0; 2731 } 2732 `, ` 2733 precision mediump float; 2734 void main(){ 2735 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 2736 } 2737 `); 2738 let srcViewPort = gl.getParameter(gl.VIEWPORT); 2739 console.log(srcViewPort); 2740 gl.useProgram(program); 2741 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); 2742 gl.activeTexture(gl.TEXTURE0); 2743 let texture = gl.createTexture(); 2744 gl.bindTexture(gl.TEXTURE_2D, texture); 2745 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 2746 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 2747 let frameBuffer = gl.createFramebuffer(); 2748 gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer); 2749 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); 2750 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 2751 gl.viewport(0, 0, 256, 256); 2752 gl.clearColor(0.9, 0.9, 0.9, 1.0); 2753 gl.clear(gl.COLOR_BUFFER_BIT); 2754 gl.drawArrays(gl.POINTS, 0, 1); 2755 2756 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 2757 let p2 = createProgram(gl, ` 2758 attribute vec4 a_Position; 2759 attribute vec2 a_TexCoord; 2760 varying vec2 v_TexCoord; 2761 void main(){ 2762 gl_Position = a_Position; 2763 v_TexCoord = a_TexCoord; 2764 } 2765 `, ` 2766 precision mediump float; 2767 uniform sampler2D u_Sampler; 2768 varying vec2 v_TexCoord; 2769 void main(){ 2770 gl_FragColor = texture2D(u_Sampler, v_TexCoord); 2771 } 2772 `); 2773 2774 gl.useProgram(p2.program); 2775 let arr = new Float32Array([ 2776 -0.5, 0.5, 0.0, 1.0, 2777 -0.5, -0.5, 0.0, 0.0, 2778 0.5, 0.5, 1.0, 1.0, 2779 0.5, -0.5, 1.0, 0.0, 2780 ]); 2781 let FSIZE = arr.BYTES_PER_ELEMENT; 2782 let buffer = gl.createBuffer(); // 创建缓冲区 2783 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); // 绑定缓冲区 2784 gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW); // 将数据写入缓冲区对象 2785 let a_Position = gl.getAttribLocation(p2.program, 'a_Position'); // 获取attribute变量地址 2786 gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0); // 将缓冲区对象分配给一个attribute变量 2787 gl.enableVertexAttribArray(a_Position); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 2788 let a_TexCoord = gl.getAttribLocation(p2.program, 'a_TexCoord'); // 获取attribute变量地址 2789 gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2); // 将缓冲区对象分配给一个attribute变量 2790 gl.enableVertexAttribArray(a_TexCoord); // 开启attribute变量(连接a_Position变量与分配给它的缓冲区对象) 2791 2792 gl.bindTexture(gl.TEXTURE_2D, texture); 2793 let u_Sampler = gl.getUniformLocation(p2.program, 'u_Sampler'); 2794 gl.uniform1i(u_Sampler, 0); // 将0号纹理传递给着色器 2795 gl.clearColor(1.92, 0.92, 0.92, 1); 2796 gl.clear(gl.COLOR_BUFFER_BIT); 2797 gl.drawArrays(gl.TRIANGLE_STRIP, 0, arr.length / 4); 2798 gl.flush(); 2799 gl.viewport(srcViewPort[0], srcViewPort[1], srcViewPort[2], srcViewPort[3]); 2800 gl.disableVertexAttribArray(a_Position); 2801 gl.disableVertexAttribArray(a_TexCoord); 2802 gl.bindTexture(gl.TEXTURE_2D, null); 2803 gl.bindBuffer(gl.ARRAY_BUFFER, null); 2804 gl.deleteBuffer(buffer); 2805 gl.deleteFramebuffer(frameBuffer); 2806 gl.deleteTexture(texture); 2807 gl.deleteShader(vertexShader); 2808 gl.deleteShader(fragmentShader); 2809 gl.deleteProgram(program); 2810 gl.deleteShader(p2.vertexShader); 2811 gl.deleteShader(p2.fragmentShader); 2812 gl.deleteProgram(p2.program); 2813 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 2814 done(); 2815 }) 2816 2817 2818 /** 2819 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0067 2820 * @tc.name webgl_test_framebufferTexture2D_1 2821 * @tc.desc Test framebufferTexture2D. 2822 */ 2823 it('webgl_test_framebufferTexture2D_1', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2824 async function (done) { 2825 framebufferTexture2D((texture) => { 2826 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, texture, 0); 2827 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 2828 }, () => { 2829 expect(checkError(gl)).assertEqual(gl.INVALID_FRAMEBUFFER_OPERATION); 2830 }) 2831 done() 2832 }) 2833 2834 2835 /** 2836 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0068 2837 * @tc.name webgl_test_framebufferTexture2D_2 2838 * @tc.desc Test framebufferTexture2D. 2839 */ 2840 it('webgl_test_framebufferTexture2D_2', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2841 async function (done) { 2842 framebufferTexture2D((texture) => { 2843 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.TEXTURE_2D, texture, 0); 2844 expect(checkError(gl)).assertEqual(gl.NO_ERROR); 2845 }, () => { 2846 expect(checkError(gl)).assertEqual(gl.INVALID_FRAMEBUFFER_OPERATION); 2847 }) 2848 done() 2849 }) 2850 2851 2852 /** 2853 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0069 2854 * @tc.name webgl_test_framebufferTexture2D_3 2855 * @tc.desc Test framebufferTexture2D. 2856 */ 2857 it('webgl_test_framebufferTexture2D_3', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2858 async function (done) { 2859 framebufferTexture2D((texture) => { 2860 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, 2861 gl.TEXTURE_CUBE_MAP_POSITIVE_X, texture, 0); 2862 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2863 }, () => { 2864 expect(checkError(gl)).assertEqual(gl.INVALID_FRAMEBUFFER_OPERATION); 2865 }) 2866 done() 2867 }) 2868 2869 2870 /** 2871 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0070 2872 * @tc.name webgl_test_framebufferTexture2D_4 2873 * @tc.desc Test framebufferTexture2D. 2874 */ 2875 it('webgl_test_framebufferTexture2D_4', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2876 async function (done) { 2877 framebufferTexture2D((texture) => { 2878 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, 2879 gl.TEXTURE_CUBE_MAP_NEGATIVE_X, texture, 0); 2880 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2881 }, () => { 2882 expect(checkError(gl)).assertEqual(gl.INVALID_FRAMEBUFFER_OPERATION); 2883 }) 2884 done() 2885 }) 2886 2887 2888 /** 2889 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0071 2890 * @tc.name webgl_test_framebufferTexture2D_5 2891 * @tc.desc Test framebufferTexture2D. 2892 */ 2893 it('webgl_test_framebufferTexture2D_5', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2894 async function (done) { 2895 framebufferTexture2D((texture) => { 2896 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, 2897 gl.TEXTURE_CUBE_MAP_POSITIVE_Y, texture, 0); 2898 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2899 }, () => { 2900 expect(checkError(gl)).assertEqual(gl.INVALID_FRAMEBUFFER_OPERATION); 2901 }) 2902 done() 2903 }) 2904 2905 2906 /** 2907 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0072 2908 * @tc.name webgl_test_framebufferTexture2D_6 2909 * @tc.desc Test framebufferTexture2D. 2910 */ 2911 it('webgl_test_framebufferTexture2D_6', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2912 async function (done) { 2913 framebufferTexture2D((texture) => { 2914 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, 2915 gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, texture, 0); 2916 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2917 }, () => { 2918 expect(checkError(gl)).assertEqual(gl.INVALID_FRAMEBUFFER_OPERATION); 2919 }) 2920 done() 2921 }) 2922 2923 2924 /** 2925 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0073 2926 * @tc.name webgl_test_framebufferTexture2D_7 2927 * @tc.desc Test framebufferTexture2D. 2928 */ 2929 it('webgl_test_framebufferTexture2D_7', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2930 async function (done) { 2931 framebufferTexture2D((texture) => { 2932 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, 2933 gl.TEXTURE_CUBE_MAP_POSITIVE_Z, texture, 0); 2934 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2935 }, () => { 2936 expect(checkError(gl)).assertEqual(gl.INVALID_FRAMEBUFFER_OPERATION); 2937 }) 2938 done() 2939 }) 2940 2941 2942 /** 2943 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0074 2944 * @tc.name webgl_test_framebufferTexture2D_8 2945 * @tc.desc Test framebufferTexture2D. 2946 */ 2947 it('webgl_test_framebufferTexture2D_8', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2948 async function (done) { 2949 framebufferTexture2D((texture) => { 2950 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, 2951 gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, texture, 0); 2952 expect(checkError(gl)).assertEqual(gl.INVALID_OPERATION); 2953 }, () => { 2954 expect(checkError(gl)).assertEqual(gl.INVALID_FRAMEBUFFER_OPERATION); 2955 }) 2956 done() 2957 }) 2958 2959 2960 /** 2961 * @tc.number GRAPHIC_FUNCTION_JS_WEBGL_TEXTURE_0075 2962 * @tc.name webgl_test_framebufferTexture2D_Error 2963 * @tc.desc Test framebufferTexture2D. 2964 */ 2965 it('webgl_test_framebufferTexture2D_Error', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, 2966 async function (done) { 2967 let texture = gl.createTexture(); 2968 console.info("webgltest [webgl_test_framebufferTexture2D] framebufferTexture2D"); 2969 console.info("webgltest [webgl_test_framebufferTexture2D] framebufferTexture2D no target"); 2970 gl.framebufferTexture2D(undefined, gl.DEPTH_STENCIL_ATTACHMENT, gl.TEXTURE_2D, texture, 0) 2971 checkError(gl) 2972 console.info("webgltest [webgl_test_framebufferTexture2D] framebufferTexture2D no attachment"); 2973 gl.framebufferTexture2D(gl.FRAMEBUFFER, undefined, gl.TEXTURE_2D, texture, 0) 2974 checkError(gl) 2975 console.info("webgltest [webgl_test_framebufferTexture2D] framebufferTexture2D no textarget"); 2976 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, undefined, texture, 0) 2977 checkError(gl) 2978 console.info("webgltest [webgl_test_framebufferTexture2D] framebufferTexture2D no texture"); 2979 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.TEXTURE_2D, undefined, 0) 2980 checkError(gl) 2981 console.info("webgltest [webgl_test_framebufferTexture2D] framebufferTexture2D no texture"); 2982 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.TEXTURE_2D, texture, undefined) 2983 checkError(gl) 2984 gl.deleteTexture(texture); 2985 done() 2986 }) 2987 }) 2988} 2989