# How to display triangles with WebGPU + WGSL?

**URL:** <https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427>\
**Category:** Questions\
**Tags:** webgpu, wgsl\
**Created:** [January 16, 2023, 5:47pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427 "2023-01-16T17:47:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cx20](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cx20/32/46_2.png) [@cx20](https://forum.babylonjs.com/u/cx20)\
**Post date:** [January 16, 2023, 5:47pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/1 "2023-01-16T17:47:35Z")

</div>

Below is a sample triangle using `GLSL` in Babylon.js. This does not seem to be a problem.

> **[Babylon.js Playground](https://playground.babylonjs.com/#HPKUXF%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

```javascript
BABYLON.ShaderStore.ShadersStore["customTriangleVertexShader"] = `#version 300 es
    in vec3 position;
    in vec4 color;
    out vec4 vColor;

    void main() {
        vColor = color;
        gl_Position = vec4(position, 1.0);
    }
`;

```

Next I tried the above sample with `WebGPU` + `WGSL`.

> **[Babylon.js Playground](https://playground.babylonjs.com/#C2C1S5%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

```javascript
BABYLON.ShaderStore.ShadersStoreWGSL["customTriangleVertexShader"] = `   
    attribute position : vec3<f32>;
    attribute color : vec4<f32>;
    varying vColor : vec4<f32>;

    @vertex
    fn main(input : VertexInputs) -> FragmentInputs {
        vColor = color;
        gl_Position = vec4<f32>(position, 1.0);
    }
`;

```

However, the following error occurred and could not be displayed.

```auto
Attribute shader location (1) is used more than once.

```

Can someone tell me what is wrong?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 16, 2023, 5:53pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/2 "2023-01-16T17:53:30Z")

</div>

Assigned @Evgeni\_Popov on January 16, 2023, 5:53pm UTC



---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 16, 2023, 5:53pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/3 "2023-01-16T17:53:48Z")

</div>

Something has been broken in the WGSL support, looking into that…

---

<div class="post-metadata">

**Author:** ![cx20](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cx20/32/46_2.png) [@cx20](https://forum.babylonjs.com/u/cx20)\
**Post date:** [January 16, 2023, 6:00pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/4 "2023-01-16T18:00:09Z")

</div>

BTW, it was displayed without problems when vertex colors were not used.

> **[Babylon.js Playground](https://playground.babylonjs.com/#C2C1S5)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 18, 2023, 11:59am UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/5 "2023-01-18T11:59:21Z")

</div>

Ok, so the **position** and **color** attributes are handled automatically by the system (as well as **normal** and **uv** ), you don’t need to list them in the `attributes` property when creating a `ShaderMaterial` instance:

> **[Babylon.js Playground](https://playground.babylonjs.com/#C2C1S5%234)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

If you do pass some attributes to the constructor, you should still don’t pass **color** because this one is automatically added as long as there is a “color” vertex buffer attached to the mesh. So, you should only pass **position** :

> **[Babylon.js Playground](https://playground.babylonjs.com/#C2C1S5%235)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

It’s a bit weird, but that’s how `ShaderMaterial` works and we can’t change it to avoid breaking backwards compatibility.

---

<div class="post-metadata">

**Author:** ![jeremy-coleman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeremy-coleman/32/15493_2.png) [@jeremy-coleman](https://forum.babylonjs.com/u/jeremy-coleman)\
**Post date:** [January 18, 2023, 2:16pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/6 "2023-01-18T14:16:05Z")

</div>

What do you think about using [GitHub - brendan-duncan/wgsl\_reflect: A WebGPU Shading Language parser and reflection library for Javascript.](https://github.com/brendan-duncan/wgsl_reflect) to automate this?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 18, 2023, 2:37pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/7 "2023-01-18T14:37:07Z")

</div>

We could not use it because we are using our own parser to parse some specific constructs not available in WGSL (“attribute” / “varying” for eg).

---

<div class="post-metadata">

**Author:** ![cx20](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cx20/32/46_2.png) [@cx20](https://forum.babylonjs.com/u/cx20)\
**Post date:** [January 18, 2023, 3:10pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/8 "2023-01-18T15:10:33Z")

</div>

It’s a little strange, but I understand that it is not necessary to specify the position and color attributes when using ShaderMaterial.  
I thought it would be nice to have some additional information about vertex colors in the documentation.

> **[Writing shaders for WebGPU in WGSL | Babylon.js Documentation](https://doc.babylonjs.com/setup/support/webGPU/webGPUWGSL)**
>
> Explain how to use WGSL in shaders when using the WebGPU engine

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 18, 2023, 3:53pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/9 "2023-01-18T15:53:40Z")

</div>

Doc updated:

> <https://github.com/BabylonJS/Documentation/pull/755>
>
> See https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/3742…7/7

---

<div class="post-metadata">

**Author:** ![cx20](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cx20/32/46_2.png) [@cx20](https://forum.babylonjs.com/u/cx20)\
**Post date:** [January 18, 2023, 5:09pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/10 "2023-01-18T17:09:55Z")

</div>

Thank you for adding to the documentation.  
I made some samples using Babylon.js and WGSL. It seems fine for now.

Colored Cube

> **[Babylon.js Playground](https://playground.babylonjs.com/#C2C1S5%236)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Textured Cube

> **[Babylon.js Playground](https://playground.babylonjs.com/#C2C1S5%237)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Teapot

> **[Babylon.js Playground](https://playground.babylonjs.com/#C2C1S5%238)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 18, 2023, 11:55pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/11 "2023-01-18T23:55:54Z")

</div>

Unassigned @Evgeni\_Popov on January 18, 2023, 11:55pm UTC



---

<div class="post-metadata">

**Author:** ![cx20](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cx20/32/46_2.png) [@cx20](https://forum.babylonjs.com/u/cx20)\
**Post date:** [October 23, 2023, 3:38pm UTC](https://forum.babylonjs.com/t/how-to-display-triangles-with-webgpu-wgsl/37427/12 "2023-10-23T15:38:57Z")

</div>

BTW, before I knew it, the previous sample stopped working. This was a sample before the official specifications were released, so the specifications may have changed.  
I have attached the modified code for your reference.

Colored Cube

> **[Babylon.js Playground](https://playground.babylonjs.com/?webgpu#C2C1S5%2310)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

```auto
BABYLON.ShaderStore.ShadersStoreWGSL["customCubeVertexShader"] = `   
    #include<sceneUboDeclaration>
    #include<meshUboDeclaration>

    attribute position : vec3<f32>;
    attribute color : vec4<f32>;

    varying vColor : vec4<f32>;

    @vertex
    fn main(input : VertexInputs) -> FragmentInputs {
        vertexOutputs.vColor = vertexInputs.color;
        vertexOutputs.position = scene.viewProjection * mesh.world * vec4<f32>(vertexInputs.position, 1.0);
    }
`;

BABYLON.ShaderStore.ShadersStoreWGSL["customCubeFragmentShader"] =`
    varying vColor : vec4<f32>;

    @fragment
    fn main(input : FragmentInputs) -> FragmentOutputs {
        fragmentOutputs.color = fragmentInputs.vColor;
    }
`;

```

Textured Cube

> **[Babylon.js Playground](https://playground.babylonjs.com/?webgpu#C2C1S5%239)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

```auto
BABYLON.ShaderStore.ShadersStoreWGSL["customCubeVertexShader"] = `   
    #include<sceneUboDeclaration>
    #include<meshUboDeclaration>

    attribute position : vec3<f32>;
    attribute uv: vec2<f32>;

    varying vUV : vec2<f32>;

    @vertex
    fn main(input : VertexInputs) -> FragmentInputs {
        vertexOutputs.vUV =vertexInputs.uv;
        vertexOutputs.position = scene.viewProjection * mesh.world * vec4<f32>(vertexInputs.position, 1.0);
    }
`;

BABYLON.ShaderStore.ShadersStoreWGSL["customCubeFragmentShader"] =`
    varying vUV : vec2<f32>;

    var diffuse : texture_2d<f32>;
    var mySampler : sampler;

    @fragment
    fn main(input : FragmentInputs) -> FragmentOutputs {
        fragmentOutputs.color = textureSample(diffuse, mySampler, fragmentInputs.vUV);
    }
`;

```

Teapot

> **[Babylon.js Playground](https://playground.babylonjs.com/?webgpu#C2C1S5%2311)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

```auto
BABYLON.ShaderStore.ShadersStoreWGSL["teapotVertexShader"] = `   
    #include<sceneUboDeclaration>
    #include<meshUboDeclaration>

    attribute position : vec3<f32>;
    attribute normal : vec3<f32>;
    attribute uv: vec2<f32>;

    varying vPosition : vec3<f32>;
    varying vNormal : vec3<f32>;
    varying vUV : vec2<f32>;

    @vertex
    fn main(input : VertexInputs) -> FragmentInputs {
        vertexOutputs.position = scene.viewProjection * mesh.world * vec4<f32>(vertexInputs.position, 1.0);
        vertexOutputs.vPosition = vertexOutputs.position.xyz;
        vertexOutputs.vNormal = vertexInputs.normal;
        vertexOutputs.vUV = vertexInputs.uv;
    }
`;

BABYLON.ShaderStore.ShadersStoreWGSL["teapotFragmentShader"] =`
    uniform uPointLightingLocation : vec3<f32>;

    varying vPosition : vec3<f32>;
    varying vNormal : vec3<f32>;
    varying vUV : vec2<f32>;

    var diffuse : texture_2d<f32>;
    var mySampler : sampler;

    @fragment
    fn main(input : FragmentInputs) -> FragmentOutputs {
        var lightDirection: vec3<f32> = normalize(uniforms.uPointLightingLocation - fragmentInputs.vPosition.xyz);
        var normal: vec3<f32> = normalize(fragmentInputs.vNormal);
        var lightWeighting: f32 = max(dot(normal, lightDirection), 0.0);
        var fragmentColor: vec4<f32> = textureSample(diffuse, mySampler, fragmentInputs.vUV);
        fragmentOutputs.color = vec4<f32>(fragmentColor.rgb * lightWeighting, fragmentColor.a);
    }
`;

```
