Scoop API

scoop:scoop.graphics:0.1.0

src/native/gpu_buffer.scoop

Selected production source · src/native/gpu_buffer.scoop

1package scoop.graphics.native
3import scoop.graphics.*
5/** A renderable native texture on the device's context; snapshots remain on the GPU. */
6public class GpuBuffer internal constructor(
7 private val target: RenderTarget,
8 public val format: NativeGpuFormat,
9 public val layout: GpuPixelLayout,
10 public val colorSpace: GpuColorSpace
11): Closeable {
12 public val pixelWidth: Int get() = target.pixelWidth
13 public val pixelHeight: Int get() = target.pixelHeight
14 public val scale: Double get() = target.scale
15 public val isClosed: Boolean get() = target.isClosed
16 public fun <T> draw(block: (Canvas) -> T): T = target.draw(block)
17 public fun snapshot(): Image = target.compositeSnapshot()
18 /** Explicit synchronization and straight RGBA8 export. */
19 public fun readPixels(): Array<Byte> = target.readPixels()
21 /**
22 * Preserves previous snapshots before borrowing the current texture. Submit native work
23 * on the supplied queue and restore the supplied D3D12 state before returning or throwing.
24 * Do not reenter the context, suspend, cross threads, or save these handles for later use.
25 */
26 @Unsafe
27 public fun <T> withNativeWrite(block: (NativeGpuDevice, NativeGpuTexture) -> T): T =
28 target.withExclusive {
29 pointer -> @Unsafe
30 {
31 return@withExclusive GpuBorrow(
32 ownedGraphics(gpuWrite(pointer), "borrow native GPU buffer")
33 ).use {
34 access -> @Unsafe
35 {
36 val handle = access.get()
37 val device = gpuAccessDevice(handle)
38 return@use block(
39 device.native(),
40 gpuAccessTexture(handle).native(device.backend())
41 )
42 }
43 }
44 }
45 }
47 public override fun close() {
48 target.close()
49 }
52@Extern(name = "scoop_graphics_gpu_write")
53private fun gpuWrite(target: Ptr<Unit>): Ptr<Unit>?
55@Extern(name = "scoop_graphics_gpu_access_texture")
56private fun gpuAccessTexture(access: Ptr<Unit>): GpuTextureInfo