GithubHelp home page GithubHelp logo

[Code Review] Possible improvement of webgl-util.js, why not use the data type from shader when setting attributes about webgl-fundamentals HOT 1 OPEN

GuichiZhao avatar GuichiZhao commented on May 24, 2024
[Code Review] Possible improvement of webgl-util.js, why not use the data type from shader when setting attributes

from webgl-fundamentals.

Comments (1)

GuichiZhao avatar GuichiZhao commented on May 24, 2024

For those who also have a similar concern, here is the conclusion I come to:
infering the data type for attribute from shader code is simply NOT possible

I tried:

attribute ivec4 a_color;
varying ivec4 v_color;

and the shader fail to compile

Error compiling shader '[object WebGLShader]':ERROR: 0:5: 'attribute' : cannot be bool or int
ERROR: 0:6: 'varying' : cannot be bool or int

Actually

The attribute qualifier can be used only with the data types float, vec2, vec3, vec4, mat2, mat3, and mat4. Attribute variables cannot be declared as arrays or structures.

However, when we specify the layout of the buffer, integer or byte is a legal option:

gl.vertexAttribPointer(a_positionLoc, positionNumComponents, gl.UNSIGNED_BYTE, false, 0, 0);

It seems that the gl engine converts all data types in the buffer to float somehow, which does not make much sense to me, maybe for the sake of hardware performance, I do not know

So, there is NO WAY to infer the individual data type in the buffer memory layout from the shader code, however, there is STILL something we can improve, we can infer the numComponents from shader! All the possible data type is listed below:
float, vec2, vec3, vec4, mat2, mat3, and mat4.
We can definitely infer numComponents

However, as mentioned in the article

Why don't we look at the attributes on the shader program to figure out the number of components? That's because it's common to supply 3 components (x, y, z) from a buffer but use a vec4 in the shader. For attributes WebGL will set w = 1 automatically. But that means we can't easily know the user's intent since what they declared in their shader might not match the number of components they provide.

So, inferring numComponents is also not possible!

from webgl-fundamentals.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.