Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange orders of this tutorial #28

Open
cangSDARM opened this issue Jun 26, 2023 · 2 comments
Open

Strange orders of this tutorial #28

cangSDARM opened this issue Jun 26, 2023 · 2 comments

Comments

@cangSDARM
Copy link

cangSDARM commented Jun 26, 2023

Sorry I am a newbie

For one's intuition, we should first talk about vertex buffer and index buffer, then storage buffer, and finally the uniform buffer. This is because vertex buffers are the simplest one. Then may followed by "globally available" storage buffers, and finally special storage buffer: uniform buffers.

Now the order is very strange, after reading those buffers, I am very confused, and even went to look up why we need vertex buffers when we have uniform buffers - I don't understand why this ordered?

@greggman
Copy link
Collaborator

greggman commented Jun 26, 2023

storage buffers are simpler than vertex buffers and index buffers.

Compare the code, it takes many more lines of code to use vertex buffers as you have to describe their shape to the API. With a storage buffer you just declare a struct. With a vertex buffer you declare a struct but every field needs a location and then in code you also have to describe the struct to the API when creating a render pipeline

  const pipeline = device.createRenderPipeline({
    label: 'lighting',
    layout: 'auto',
    vertex: {
      module: shaderModule,
      entryPoint: 'myVSMain',
      buffers: [
        {
          arrayStride: (3 + 3 + 2) * 4, // position, normal, texcoord
          attributes: [
            {shaderLocation: 0, offset: 0 , format: 'float32x3'},  // position
            {shaderLocation: 1, offset: 12, format: 'float32x3'},  // normal
            {shaderLocation: 2, offset: 24, format: 'float32x2'},  // texcoord
          ],
        },
      ],
    },

As for why do we have vertex buffers, the main reason is they are older than storage buffers and on older GPUs they are faster. Newer GPU don't have vertex buffers, they are emulated in code.

You can find many articles saying we should get rid of vertex buffers. Here's one.

https://wickedengine.net/2017/06/05/should-we-get-rid-of-vertex-buffers/

Often this is described as "vertex pulling".

https://www.google.com/search?q=vertex+pulling

It's also more flexible. If you wanted to write a vertex shader that referenced other vertices you can with storage buffers, you can not with vertex buffers.

As for why the order, as it says at the top, they're simpler. The order is simplest to most complex. In particular, going from uniform buffers to storage buffers is a tiny change which you can see right at the top of the article

As for uniform buffers before vertex buffers, again, they are easier then vertex buffers and they are arguably more important than vertex buffers. Shaders are just functions, functions need inputs. Uniform buffers are the most common way to provide inputs. They're used in all 3 types of shaders, vertex shaders, fragment shaders, compute shaders. Vertex buffers are not, they are only used in vertex shaders.

As an example, there are no vertex buffers referenced on this entire site: https://shadertoy.com but there are uniforms. Same for this site: https://vertexshaderart.com.

@cangSDARM
Copy link
Author

Thank you for your explanation! That really convinced me from the simple to complex perspective you described.

Yet, I think this sentence definitely needs to be added to one of the three materials to explain the relationship between these three buffers and the course arrangement 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants