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