WebGPU中画一个圆

在这里插入图片描述
0.0 我这里没check环境啊,就当默认有了。刚学习webgpu,有懂哥可以多交流

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
        WebGPU Circle Example
    </title>
</head>

<body>
    <canvas id="canvas" width="900" height="900"></canvas>

    <script type="module">
        const canvas = document.getElementById("canvas");
        const adapter = await navigator.gpu.requestAdapter();
        const device = await adapter.requestDevice();
        const context = canvas.getContext("webgpu");
        const presentationFormat = navigator.gpu.getPreferredCanvasFormat();

        const width = 900;

        const shader = `
            struct Output {
                @builtin(position) Position : vec4<f32>,
                @location(0) coord : vec2<f32>,
            }

            @vertex
            fn vs_main(
               @location(0) pos: vec4<f32>, @location(1) coord: vec2<f32>
            ) -> Output {
                var output: Output;
                output.Position = pos;
                output.coord = coord;
                return output;
            }

            @fragment
            fn fs_main(
                in: Output
            ) -> @location(0) vec4<f32> {
                
                let coord = in.coord;

                let r = dot(coord, coord);

                if (r > 0.95) {
                    discard;
                }

                return vec4<f32>(1.0, 0.0, 0.0, 1.0);
            }
        `

        const CreateGPUBuffer = (device, data, usageFlag = GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST) => {
            const buffer = device.createBuffer({
                size: data.byteLength,
                usage: usageFlag,
                mappedAtCreation: true
            });
            new Float32Array(buffer.getMappedRange()).set(data);
            buffer.unmap();
            return buffer;
        }

        context.configure({
            device: device,
            format: presentationFormat,
            alphaMode: 'opaque', // or 'premultiplied'
        });


        const vertexData = new Float32Array([
            -0.5, -0.5,  
            0.5, -0.5,  
            -0.5,  0.5,  

            -0.5,  0.5,  
            0.5, -0.5,  
            0.5,  0.5,  
        ]);

        const coordData = new Float32Array([
            -1, -1 ,    
            1, -1,    
            -1, 1,    

            -1, 1, 
            1, -1,   
            1, 1,   
        ]);

        const vertexBuffer = CreateGPUBuffer(device, vertexData);
        const coordBuffer = CreateGPUBuffer(device, coordData);
            
        const queue = device.queue;

        const pipeline = device.createRenderPipeline({
            layout:'auto',
            vertex: {
                module: device.createShaderModule({                    
                    code: shader
                }),
                entryPoint: "vs_main",
                buffers:[
                    {
                        arrayStride: 2 * 4,
                        attributes: [{
                            shaderLocation: 0,
                            format: "float32x2",
                            offset: 0
                        }]
                    },
                    {
                        arrayStride: 2 * 4,
                        attributes: [{
                            shaderLocation: 1,
                            format: "float32x2",
                            offset: 0
                        }]
                    }
                ]
            },
            fragment: {
                module: device.createShaderModule({                    
                    code: shader
                }),
                entryPoint: "fs_main",
                targets: [
                    {
                        format: presentationFormat
                    }
                ]
            },
            primitive:{
                topology: "triangle-list",
            }
        });

        const commandEncoder = device.createCommandEncoder();
        const textureView = context.getCurrentTexture().createView();
        const renderPass = commandEncoder.beginRenderPass({
            colorAttachments: [{
                view: textureView,
                clearValue: {r: 0.2, g: 0.247, b: 0.314, a: 1.0}, //background color
                loadOp: 'clear',
                storeOp: 'store'
            }]
        });
        renderPass.setPipeline(pipeline);
        renderPass.setVertexBuffer(0, vertexBuffer);
        renderPass.setVertexBuffer(1, coordBuffer);
        renderPass.draw(6);
        renderPass.end();

        device.queue.submit([commandEncoder.finish()]);
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值