scoop:scoop.graphics:0.1.0
src/native/gpu_buffer.scoop
Selected production source · src/native/gpu_buffer.scoop
1
package scoop.graphics.native3
import scoop.graphics.*5
/** A renderable native texture on the device's context; snapshots remain on the GPU. */6
public class GpuBuffer internal constructor(7
private val target: RenderTarget,8
public val format: NativeGpuFormat,9
public val layout: GpuPixelLayout,10
public val colorSpace: GpuColorSpace11
): Closeable {12
public val pixelWidth: Int get() = target.pixelWidth13
public val pixelHeight: Int get() = target.pixelHeight14
public val scale: Double get() = target.scale15
public val isClosed: Boolean get() = target.isClosed16
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 work23
* 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
@Unsafe27
public fun <T> withNativeWrite(block: (NativeGpuDevice, NativeGpuTexture) -> T): T =28
target.withExclusive {29
pointer -> @Unsafe30
{31
return@withExclusive GpuBorrow(32
ownedGraphics(gpuWrite(pointer), "borrow native GPU buffer")33
).use {34
access -> @Unsafe35
{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
}50
}52
@Extern(name = "scoop_graphics_gpu_write")53
private fun gpuWrite(target: Ptr<Unit>): Ptr<Unit>?55
@Extern(name = "scoop_graphics_gpu_access_texture")56
private fun gpuAccessTexture(access: Ptr<Unit>): GpuTextureInfo