Forums - about glsl struct

1 post / 0 new
about glsl struct
zhouhai
Join Date: 14 Mar 19
Posts: 1
Posted: Thu, 2019-03-14 22:47
There are some problems with the use of glsl struct on the Android platform.this is my test code.
 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Struct constructor/initializers highp bug.</title>
</head>
<body>
<canvas id="canvas" width="256" height="256"> </canvas>
<script type="application/javascript">
"use strict";
var canvas = document.getElementById('canvas');
var gl = canvas.getContext('webgl');
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0, 0, 1, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
 
var vs = gl.createShader(gl.VERTEX_SHADER);
var vs_code =`
attribute vec4 pos;
void main(){
gl_Position = pos;
}
`;
gl.shaderSource(vs, vs_code);
gl.compileShader(vs);
 
var fs = gl.createShader(gl.FRAGMENT_SHADER);
var fs_code =`
precision highp float;
struct Test {
vec3 color;
};
void main() {
vec3 color = vec3( 6378137.0, 0.0, 0.0 );
Test test;
test.color = color;
if (test.color.x == 6000000.0) {
gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );
} else {
gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 );
}
}
`;
gl.shaderSource(fs, fs_code);
gl.compileShader(fs);
 
var prg = gl.createProgram();
gl.attachShader(prg, vs);
gl.attachShader(prg, fs);
gl.linkProgram(prg);
gl.useProgram(prg);
 
var posLocation = gl.getAttribLocation(prg, 'pos');
gl.enableVertexAttribArray(posLocation);
 
var vertex = new Float32Array([
-0.8, -0.8, 0,
-0.8, 0.8, 0,
0.8, 0.8, 0,
0.8, -0.8, 0
]);
 
var index = new Uint8Array([0, 2, 1, 0, 3, 2]);
 
var vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertex, gl.STATIC_DRAW);
gl.vertexAttribPointer(posLocation, 3, gl.FLOAT, false, 0, 0);
 
var indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, index, gl.STATIC_DRAW);
gl.drawElements(gl.TRIANGLES, index.length, gl.UNSIGNED_BYTE, 0);
</script>
</body>
</html>
 
This program will display a green square on the pc, but will display a red square on Android, the phone processor is Snapdragon 845, the browser is chrome

 

  • Up0
  • Down0

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries (“Qualcomm”). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.