WebGPU Demo var script document createElement script script type text javascript script async false script src https cdnjs cloudflare com ajax libs gl matrix 2 6 0 gl matrix min js document head appendChild script const canvas document createElement canvas document body appendChild canvas canvas width canvas height 512 console log canvas width canvas height const context canvas getContext webgpu const gpu navigator gpu console log gpu gpu const adapter await gpu requestAdapter const device await adapter requestDevice const devicePixelRatio window devicePixelRatio 1 const presentationSize canvas clientWidth devicePixelRatio canvas clientHeight devicePixelRatio const presentationFormat context getPreferredFormat adapter console log presentationFormat context configure device device format presentationFormat size presentationSize var vertWGSL struct VSOut builtin position Position vec4 f32 location 0 uvs vec2 f32 stage vertex fn main builtin vertex_index VertexIndex u32 VSOut var s 0 9 var pos array vec2 f32 4 vec2 f32 s s vec2 f32 s s vec2 f32 s s vec2 f32 s s s 1 0 var uvs array vec2 f32 4 vec2 f32 s s vec2 f32 s s vec2 f32 s s vec2 f32 s s var vsOut VSOut vsOut Position vec4 f32 pos VertexIndex 0 0 1 0 vsOut uvs uvs VertexIndex 0 5 0 5 0 1 return vsOut var fragWGSL block struct Uniforms mytimer f32 group 0 binding 0 var mySampler sampler group 0 binding 1 var myTexture texture_2d f32 group 0 binding 2 var uniform uniforms Uniforms fn distanceToMandelbrot c vec2 f32 f32 var di 1 0 var z vec2 f32 0 0 var m2 0 0 var dz vec2 f32 0 0 for var i 0 i 300 i i 1 if m2 1024 0 di 0 0 break Z 2ZZ 1 dz 2 0 vec2 f32 z x dz x z y dz y z x dz y z y dz x vec2 f32 1 0 0 0 Z Z c z vec2 f32 z x z x z y z y 2 0 z x z y c m2 dot z z distance d c Z log Z Z var d 0 5 sqrt dot z z dot dz dz log dot z z if di 0 5 d 0 0 return d stage fragment fn main location 0 uvs vec2 f32 location 0 vec4 f32 var background1Color textureSample myTexture mySampler uvs xyz var iTime uniforms mytimer var p 2 0 uvs animation var tz 0 5 0 5 cos 0 225 iTime var zoo pow 0 5 13 0 tz var c vec2 f32 0 05 6805 p zoo distance to Mandelbrot var d distanceToMandelbrot c do some soft coloring based on distance d clamp pow 4 0 d zoo 0 2 0 0 1 0 var col vec3 f32 d var fragColor vec4 f32 col 1 0 return fragColor let textureSampler device createSampler minFilter linear magFilter linear const img document createElement img img src https notebook xbdev net var images test512 png await img decode const basicTexture device createTexture size img width img height 1 format presentationFormat bgra8unorm rgba8unorm usage 0x2 0x4 GPUTextureUsage COPY_DST GPUTextureUsage TEXTURE_BINDING const imageCanvas document createElement canvas imageCanvas width img width imageCanvas height img height const imageCanvasContext imageCanvas getContext 2d imageCanvasContext drawImage img 0 0 imageCanvas width imageCanvas height const imageData imageCanvasContext getImageData 0 0 imageCanvas width imageCanvas height let textureData new Uint8Array img width img height 4 for let x 0 x img width img height 4 x textureData x imageData data x device queue writeTexture texture basicTexture textureData offset 0 bytesPerRow img width 4 rowsPerImage img height img width img height 1 const timerUniformBuffer device createBuffer size 4 usage GPUBufferUsage UNIFORM GPUBufferUsage COPY_DST const timestep new Float32Array 3 14 device queue writeBuffer timerUniformBuffer 0 timestep const sceneUniformBindGroupLayout device createBindGroupLayout entries binding 0 visibility GPUShaderStage FRAGMENT sampler type filtering binding 1 visibility GPUShaderStage FRAGMENT texture sampleType float viewDimension 2d binding 2 visibility GPUShaderStage FRAGMENT buffer type uniform const uniformBindGroup device createBindGroup layout sceneUniformBindGroupLayout entries binding 0 resource textureSampler binding 1 resource basicTexture createView binding 2 resource buffer timerUniformBuffer const pipeline device createRenderPipeline layout device createPipelineLayout bindGroupLayouts sceneUniformBindGroupLayout vertex module device createShaderModule code vertWGSL entryPoint main fragment module device createShaderModule code fragWGSL entryPoint main targets format presentationFormat primitive topology triangle strip frontFace ccw cullMode back stripIndexFormat uint32 GPURenderPassDescriptor const renderPassDescriptor colorAttachments view undefined asign later in frame loadValue r 0 0 g 0 5 b 0 5 a 1 0 storeOp store function frame timestep 0 0 01 device queue writeBuffer timerUniformBuffer 0 timestep renderPassDescriptor colorAttachments 0 view context getCurrentTexture createView const commandEncoder device createCommandEncoder const renderPass commandEncoder beginRenderPass renderPassDescriptor renderPass setPipeline pipeline renderPass setBindGroup 0 uniformBindGroup renderPass draw 4 1 0 0 renderPass endPass device queue submit commandEncoder finish if you want constant updates animated keep refreshing requestAnimationFrame frame frame console log ready
img width img height 1 format presentationFormat bgra8unorm rgba8unorm usage 0x2 0x4 GPUTextureUsage COPY_DST GPUTextureUsage TEXTURE_BINDING const imageCanvas document createElement canvas imageCanvas width img width imageCanvas height img height const imageCanvasContext imageCanvas getContext 2d imageCanvasContext drawImage img 0 0 imageCanvas width imageCanvas height const imageData imageCanvasContext getImageData 0 0 imageCanvas width imageCanvas height let textureData new Uint8Array img width img height 4 for let x 0 x img width img height 4 x textureData x imageData data x device queue writeTexture texture basicTexture textureData offset 0 bytesPerRow img width 4 rowsPerImage img height img width img height 1 const timerUniformBuffer device createBuffer size 4 usage GPUBufferUsage UNIFORM GPUBufferUsage COPY_DST const timestep new Float32Array 3 14 device queue writeBuffer timerUniformBuffer 0 timestep const sceneUniformBindGroupLayout device createBindGroupLayout entries binding 0 visibility GPUShaderStage FRAGMENT sampler type filtering binding 1 visibility GPUShaderStage FRAGMENT texture sampleType float viewDimension 2d binding 2 visibility GPUShaderStage FRAGMENT buffer type uniform const uniformBindGroup device createBindGroup layout sceneUniformBindGroupLayout entries binding 0 resource textureSampler binding 1 resource basicTexture createView binding 2 resource buffer timerUniformBuffer const pipeline device createRenderPipeline layout device createPipelineLayout bindGroupLayouts sceneUniformBindGroupLayout vertex module device createShaderModule code vertWGSL entryPoint main fragment module device createShaderModule code fragWGSL entryPoint main targets format presentationFormat primitive topology triangle strip frontFace ccw cullMode back stripIndexFormat uint32 GPURenderPassDescriptor const renderPassDescriptor colorAttachments view undefined asign later in frame loadValue r 0 0 g 0 5 b 0 5 a 1 0 storeOp store function frame timestep 0 0 01 device queue writeBuffer timerUniformBuffer 0 timestep renderPassDescriptor colorAttachments 0 view context getCurrentTexture createView const commandEncoder device createCommandEncoder const renderPass commandEncoder beginRenderPass renderPassDescriptor renderPass setPipeline pipeline renderPass setBindGroup 0 uniformBindGroup renderPass draw 4 1 0 0 renderPass endPass device queue submit commandEncoder finish if you want constant updates animated keep refreshing requestAnimationFrame frame frame console log ready