GPU vertex format array with each element specifying the GPUVertexFormat
of teh attribute.
The offset array that is optional. The offset, in bytes, is counted from the beginning of the element to the data
for the attribute. Note that the offset must be a multiple of the minimum of 4 and sizeof the attrib.format
.
The stride, in bytes, between elements of the array. This is an optional argument.
The numeric location associated with the attribute, such as position, normal, or uv, which will
correspond with a @location
attribute declared in the vertex shader. This is an optional argument.
Generated using TypeDoc
This function sets the
buffers
attribute of the vertex state in a render pipeline. In this function, the input argumentformats
is a GPU vertex-format array. It can be specified as'float32'
,'float32x2'
,'float32x3'
,'float32x4'
, etc., which correspond to the WGSL style in the shaderf32
,vec2<f32>
,vec3<f32>
,vec4<f32>
, etc. If the vertex data is stored in a separate buffer for each attribute such as position, normal, and UV, you can simply provide only this input argument like['float32x3', 'float32x3', 'float32x2']
and ignore all the other optional arguments. In this case, thesetVertexBuffers
function will automatically calculate theoffset
,arrayStride
, andshaderLocation
for each vertex attribute. Note that theshaderLocation
is set with an array filled with consecutive numbers like [0, 1, 2], which must match the@location
attribute specified in the vertex shader. Otherwise, you need to manually specify theshaderLocations
array argument.On the other hand, if you store the vertex data in a single buffer for all attributes (e.g., position, normal, and uv), you will need to provide not only the vertex
formats
array, but also theoffsets
array. Here is an example of a single buffer that stores theposition
(vec3<f32>
),normal
(vec3<f32>
), anduv
(vec2<f32>
) data. The correspondingarrayStride
will be 12, 12, and 8, and theoffsets
array will be [0, 12, 24]. In this case, you can set thebuffers
attribute by calling the function like this:const bufs = setVertexBuffers(['float32x3', 'float32x3', 'float32x2'], [0, 12, 24]);
The above example assumes that all the vertex attributes (position, normal, and uv) stored in a single buffer are used in the pipeline and vertex shader. What happens if not all the attributes in the buffer are needed. For example, the pipeline and shader only need the
position
anduv
data, but not thenormal
data. In this case, in addition to theformats
andoffsets
arguments, you will also need to specify thetotalArrayStride
argument. ThearrayStride
forposition
,normal
, anduv
is 12, 12, and 8, respectively, so thetotalArrayStride
= 12 + 12 + 8 = 32. Thus, we can create thebuffers
attribute using the following codeconst bufs = setVertexBuffers(['float32x3', 'float32x2'], [0, 24], 32);
Note that the
offsets
array is set to [0, 24] rather than [0, 12], because theuv
data starts afterposition
andnormal
data, while thenormal
data is still stored in the buffer even though it is not used in this example.Returns
An array of GPU vertex buffer layout.