www.xbdev.net
xbdev - software development
Thursday February 19, 2026
Home | Contact | Support | WebGPU Graphics and Compute ... | WebGPU.. Games, Tutorials, Demos, Projects, and Code.....
     
 

WebGPU..

Games, Tutorials, Demos, Projects, and Code.....

 


Texture Transparency (Alpha/Blending)


Textures can have alpha values. 32bit textures consist if red, green, blue and alpha values. We want to enable our WebGPU renderer so these alpha values are not ignored. As up to know, you might not have noticed - if you load in a texture and it has alpha - it will just be ingnored.


Loaded texture mapped to the surface of a square.
Loaded texture mapped to the surface of a square.


Functions Used: setVertexBuffer(), setIndexBuffer(), drawIndexed(), createBuffer(), getMappedRange(), getContext(), requestAdapter(), getPreferredCanvasFormat(), createCommandEncoder(), beginRenderPass(), setPipeline(), draw(), end(), submit(), getCurrentTexture(), createView(), createShaderModule()


There are two key places that make alpha blend work (the first switches it on and the second lets you adjust the blending options).

First when you setup the canvas - set the
alphaMode'':

[XCODE]

Then in the pipeline, there is also a 
blend`` structure that goes with the fragment target:

const context canvas.getContext('webgpu');
const 
adapter await navigator.gpu.requestAdapter();
const 
device  await adapter.requestDevice();
const 
presentationFormat navigator.gpu.getPreferredCanvasFormat(); 
context.configure({ devicedeviceformatpresentationFormat,
                    
alphaMode'premultiplied',  /* IMPORTANT IMPORTANT - vs default opacity */ });



Let's bring it all together and look a the full example:

const pipeline device.createRenderPipeline({
  
layoutdevice.createPipelineLayout({bindGroupLayouts: [sceneUniformBindGroupLayout]}),
  
vertex:      {   moduleshaderModuleentryPoint'vsmain'
                   
buffers: [
                            { 
arrayStride4*3,attributes: [ {shaderLocation0offset0format'float32x3' } ] },
                            { 
arrayStride4*2,attributes: [ {shaderLocation1offset0format'float32x2' } ] }
                            ]
               },
  
fragment:    {   moduleshaderModuleentryPoint'psmain',
                   
targets: [ { formatpresentationFormat,
                                
blend: { color: {srcFactor:'src-alpha'dstFactor:"one-minus-src-alpha" ,operation:"add"},
                               
alpha: {srcFactor:'one',       dstFactor:"one" ,                operation:"add"}  }
                              
                             } ]
               }, 










Resources and Links


• WebGPU Lab Example []

WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Compute graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright wgsl webgpugems shading language cookbook kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.