WebGPU-Direct

 view release on metacpan or  search on metacpan

examples/rotatingCube.pl  view on Meta::CPAN

#!/usr/bin/env perl

# Rotating Cube, adapted from WebGPU sample:
# https://webgpu.github.io/webgpu-samples/?sample=rotatingCube

use v5.30;
use Data::Dumper;
use Time::HiRes qw/time/;
use WebGPU::Direct qw/:all/;

use Math::Trig qw/:pi tan/;
use Math::3Space qw/space vec3 perspective_projection/;

use FindBin qw/$Bin $Script/;
my $cube = require "$Bin/$Script.cube.pl";

my $wgpu = WebGPU::Direct->new;

my $width  = 600;
my $height = 600;

my $wgsl = do { local $/; <DATA> };
my ( $basicVertWGSL, $vertexPositionColorWGSL ) = split /^---\n/xms, $wgsl;

my $context = $wgpu->createSurface(
  {
    nextInChain => WebGPU::Direct->new_window( $width, $height ),
  }
);

my $adapter = $wgpu->createAdapter( { compatibleSurface => $context } );
my $device  = $adapter->createDevice;

my $presentationFormat = $context->getPreferredFormat($adapter);

$context->configure(
  {
    device => $device,
    format => $presentationFormat,
    width  => $width,
    height => $height,
  }
);

# Create a vertex buffer from the cube data.
my $verticesBuffer = $device->createBuffer(
  {
    size             => $cube->{cubeVertexCount} * 10 * BYTES_PER_f32,
    usage            => BufferUsage->vertex,
    mappedAtCreation => 1,
  }
);

$verticesBuffer->getMappedRange->buffer_f32( $cube->{cubeVertexArray}->@* );
$verticesBuffer->unmap;

my $pipeline = $device->createRenderPipeline(
  {
    layout => undef,
    vertex => {
      entryPoint => 'main',
      module     => $device->createShaderModule(
        {
          code => $basicVertWGSL,
        }
      ),
      buffers => [
        {
          arrayStride => $cube->{cubeVertexSize},
          attributes  => [
            {
              # position
              shaderLocation => 0,
              offset         => $cube->{cubePositionOffset},
              format         => 'float32x4',



( run in 0.481 second using v1.01-cache-2.11-cpan-e1769b4cff6 )