Class: WGPU::CanvasContext

Inherits:
Object
  • Object
show all
Includes:
NativeResource
Defined in:
lib/wgpu/core/canvas_context.rb,
sig/wgpu.rbs

Constant Summary

Constants included from NativeResource

NativeResource::GUARDED_METHOD_EXEMPTIONS

Instance Attribute Summary collapse

Attributes included from NativeResource

#label

Instance Method Summary collapse

Methods included from NativeResource

included, #inspect, #released?, #use

Constructor Details

#initialize(instance, present_info = {}) ⇒ CanvasContext

Creates a canvas context for platform presentation information.

Parameters:

  • instance (Instance)

    owning WebGPU instance

  • present_info (Hash) (defaults to: {})

    platform handles or an existing surface



10
11
12
13
14
15
16
# File 'lib/wgpu/core/canvas_context.rb', line 10

def initialize(instance, present_info = {})
  @instance = instance
  @present_info = present_info || {}
  @surface = @present_info[:surface]
  @physical_size = [0, 0]
  @config = nil
end

Instance Attribute Details

#physical_size[Integer, Integer] (readonly)

Returns the value of attribute physical_size.

Returns:

  • ([Integer, Integer])


5
6
7
# File 'lib/wgpu/core/canvas_context.rb', line 5

def physical_size
  @physical_size
end

Instance Method Details

#configure(device:, format: nil, usage: :render_attachment, view_formats: [], color_space: "srgb", tone_mapping: nil, alpha_mode: :opaque, width: nil, height: nil, present_mode: :fifo) ⇒ Hash

Configures the backing surface for presentation.

Parameters:

  • device (Device)

    device used to render frames

  • format (Symbol, nil) (defaults to: nil)

    surface texture format

  • (Object)

Returns:

  • (Hash)

    resolved canvas configuration

Raises:

  • (SurfaceError)

    if no surface exists or dimensions are not positive



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/wgpu/core/canvas_context.rb', line 50

def configure(device:, format: nil, usage: :render_attachment, view_formats: [], color_space: "srgb", tone_mapping: nil, alpha_mode: :opaque, width: nil, height: nil, present_mode: :fifo)
  ensure_surface

  width = width || @physical_size[0]
  height = height || @physical_size[1]
  raise SurfaceError, "Surface size must be positive before configure" if width.to_i <= 0 || height.to_i <= 0

  resolved_format = format || get_preferred_format(device.adapter)
  @surface.configure(
    device: device,
    format: resolved_format,
    usage: usage,
    width: width,
    height: height,
    present_mode: present_mode,
    alpha_mode: alpha_mode,
    view_formats: view_formats
  )
  @config = {
    device: device,
    format: resolved_format,
    usage: usage,
    view_formats: view_formats,
    color_space: color_space,
    tone_mapping: tone_mapping,
    alpha_mode: alpha_mode,
    width: width,
    height: height,
    present_mode: present_mode
  }
end

#get_configurationHash?

Returns the most recent canvas configuration.

Returns:

  • (Hash, nil)

    configuration options, or nil when unconfigured



41
42
43
# File 'lib/wgpu/core/canvas_context.rb', line 41

def get_configuration
  @config
end

#get_current_textureTexture

Acquires the texture for the current presentation frame.

Returns:

  • (Texture)

    acquired surface texture

Raises:



94
95
96
97
98
# File 'lib/wgpu/core/canvas_context.rb', line 94

def get_current_texture
  raise SurfaceError, "Canvas context must be configured before get_current_texture" unless @config

  @surface.current_texture
end

#get_preferred_format(adapter) ⇒ Symbol

Returns the surface format preferred by an adapter.

Parameters:

  • adapter (Adapter)

    adapter used to query surface capabilities

  • (Adapter)

Returns:

  • (Symbol)

    preferred texture format



33
34
35
36
# File 'lib/wgpu/core/canvas_context.rb', line 33

def get_preferred_format(adapter)
  ensure_surface
  @surface.get_preferred_format(adapter)
end

#presentvoid

This method returns an undefined value.

Presents the current surface texture.



102
103
104
# File 'lib/wgpu/core/canvas_context.rb', line 102

def present
  @surface&.present
end

#releasevoid

This method returns an undefined value.

Releases the backing surface and clears the configuration.



109
110
111
112
113
# File 'lib/wgpu/core/canvas_context.rb', line 109

def release
  @surface&.release
  @surface = nil
  @config = nil
end

#set_physical_size(width, height) ⇒ Array<Integer>

Updates the drawable's physical pixel size.

Parameters:

  • width (Integer)

    width in pixels

  • height (Integer)

    height in pixels

  • (Integer)
  • (Integer)

Returns:

  • (Array<Integer>)

    stored width and height

Raises:

  • (ArgumentError)

    if either dimension is negative



23
24
25
26
27
# File 'lib/wgpu/core/canvas_context.rb', line 23

def set_physical_size(width, height)
  raise ArgumentError, "width and height must be non-negative" if width.to_i.negative? || height.to_i.negative?

  @physical_size = [width.to_i, height.to_i]
end

#unconfigurevoid

This method returns an undefined value.

Removes the current surface configuration.



84
85
86
87
# File 'lib/wgpu/core/canvas_context.rb', line 84

def unconfigure
  @surface&.unconfigure
  @config = nil
end