-
2023/06/20
-
vertex shader: coordinate/position of vertices-
by default, every 3 vertex create a triangle
-
-
fragment shader: how to color each pixel while draw-
being called by
GPUevery time each pixel is drawn
-
-
textures: 2d array of colors, 2d image -
#wgsl
-
@vertexannotate this is avertex shader -
@fragmentannotate this is afragment shader -
TODOsomething about compute shader
-
-
interstage variable: is a variable pass betweenvertex shaderandfragment shader-
connection between
vertex shaderandfragment shaderislocation() -
those value will be interpolated between points
-
-
notes:webgpu fundamentals
-
2023/06/21
-
webgpu data memory layout
-
use
typed arrayto create amemory view-
this is a binary format
-
new ArrayBuffer(12)will create an array buffer with 12 bytes-
1 byte = 8 bits
-
index will be 0-11
-
-
new Float32Array(3)will create an array buffer with 12 bytes (3*32/8)-
32 here means 32 bits 😨 = 4 bytes per slot
-
index here will be 0-2
-
-
note:
1 byteusually a smallest unit of memory access when referring to all this stuff
-
-
every type in [[webgpu]] have alignment requirement
-
align 8means the data only start at offset multiple of 8 i.e 0, 8, 16 -
for a given type it must be aligned to a multiple of bytes
-
structandarrayhave different rules for alignment -
readmore and library to do this for us:
-
WGSL spec id:: 6492d663-b72f-4371-9e7b-97f841ad4e38
-
-
-
-
uniformsis a global variable from outside passed into shader-
step to pass
uniformintoshader-
create resource aka buffer with
device.createBuffer-
defined with
sizeandusage
-
-
create values in the host language(in this case js) with
typed array-
be sure that your data layout and alignment is correct
-
-
create
bindGroupto. bind thebuffer resourcecreate from step 1 -
copy the value from the host into
shaderusingdevice.queue.writeBuffer -
set
setBindGroupduringrenderpass
-
-
-
-
2023/06/26
-
storage buffers
-
syntax:
-
@group(x) @binding(y) var<storage,read>
-
-
different from uniform buffers
-
can both read or write
-
much larger
-
storage buffer: 128mb
-
uniform buffer: 64kb
-
-
uniform is usually faster (i think because of read only nature)
-
work for use case like passing global, reusable that repeatedly used throughout the app
-
example: orientation, material properties
-
-
-
-
-
draw()call have a second parameter isinstanceCount-
which define the number of instances to draw
-
default to 1
-
can be use to draw 100 of triangles with a single draw call
-
storage buffer is big enough to house hundreds of triangle
-
-
-
storage bufferscan be used to storevertex dataand its gaining popularity even though it’s been told to be slower on older device -
vertex buffers
-
we can draw triangle as much as we like as long as vertex shader return
@builtin(position) -
@location()is the input and output of outside world-
location(0)for input and output is completely related-
for output: it’s a canvas or
viewdefined inrender pass descriptor -
for input: it’s a location where the
vertex datacan pulled data from the outside world, defined inrender pipeline descriptor
-
-
-
-
index buffersdescribe order to draw vertices-
create an index buffer with
GPUBufferUsage.INDEX -
set index in render pass with
pass.setIndexBuffer -
draw using
pass.drawIndexedinstead -
micro optimization, save space instead of recompute same vertices
-
-