scoop:scoop.graphics:0.1.0
src/native/gpu_types.scoop
Selected production source · src/native/gpu_types.scoop
1
package scoop.graphics.native3
public enum GpuBackend {4
Metal, D3D12, GLES5
}7
/** The platform's unmodified pixel format; no format conversion or fallback is performed. */8
public enum NativeGpuFormat {9
Metal(ULong), D3D12(UInt), GLES(UInt)10
}12
/** How Skia interprets the texture's channels, independently of its native storage format. */13
public enum GpuPixelLayout {14
Rgba8, Bgra8, Rgba16F, Rgb10A2, R8, Rg815
}17
public enum GpuColorSpace {18
Srgb, LinearSrgb19
}21
public enum GpuImageAlpha {22
Premultiplied, Unpremultiplied, Opaque23
}25
public enum GpuImageOrigin {26
TopLeft, BottomLeft27
}29
/** Borrowed device and queue, or EGL display and current context. Native owners use platform retain rules. */30
public enum NativeGpuDevice {31
Metal(Ptr<Unit>, Ptr<Unit>), D3D12(Ptr<Unit>, Ptr<Unit>, Ptr<Unit>),32
GLES(Ptr<Unit>, Ptr<Unit>)33
}35
/** D3D12 includes the state which a native writer must restore before returning. */36
public enum NativeGpuTexture {37
Metal(Ptr<Unit>), D3D12(Ptr<Unit>, UInt), GLES(UInt)38
}40
/**41
* Pure native callbacks only; neither callback may enter Scoop or its GC.42
* release runs when Skia no longer uses the texture, possibly on a cleanup thread.43
* Its second argument is the final D3D12 resource state, or zero on other backends.44
*/45
@CLayout46
public struct NativeGpuImageOwner(47
val payload: Ptr<Unit>,48
val retain: GCLeafFunPtr<(Ptr<Unit>) -> Unit>,49
val release: GCLeafFunPtr<(Ptr<Unit>, UInt) -> Unit>50
)52
internal fun NativeGpuFormat.backend(): GpuBackend =53
when (this) {54
case Metal(_) -> GpuBackend.Metal55
case D3D12(_) -> GpuBackend.D3D1256
case GLES(_) -> GpuBackend.GLES57
}59
internal fun NativeGpuFormat.code(): ULong =60
when (this) {61
case Metal(val value) -> value62
case D3D12(val value) -> value.toULong()63
case GLES(val value) -> value.toULong()64
}66
internal fun GpuPixelLayout.code(): Int =67
when (this) {68
case Rgba8 -> 069
case Bgra8 -> 170
case Rgba16F -> 271
case Rgb10A2 -> 372
case R8 -> 473
case Rg8 -> 574
}76
internal fun GpuImageAlpha.code(): Int =77
when (this) {78
case Premultiplied -> 079
case Unpremultiplied -> 180
case Opaque -> 281
}83
internal fun NativeGpuTexture.backend(): GpuBackend =84
when (this) {85
case Metal(_) -> GpuBackend.Metal86
case D3D12(_, _) -> GpuBackend.D3D1287
case GLES(_) -> GpuBackend.GLES88
}90
internal fun NativeGpuTexture.info(): GpuTextureInfo =91
when (this) {92
case Metal(val texture) -> GpuTextureInfo(Some(texture), 0u, 0u)93
case D3D12(val texture, val state) -> GpuTextureInfo(Some(texture), 0u, state)94
case GLES(val name) -> GpuTextureInfo(None, name, 0u)95
}97
@CLayout98
internal struct GpuDeviceInfo(99
val kind: Int,100
val first: Ptr<Unit>?,101
val second: Ptr<Unit>?,102
val third: Ptr<Unit>?103
) {104
fun backend(): GpuBackend =105
when (kind) {106
case 1 -> GpuBackend.Metal107
case 2 -> GpuBackend.D3D12108
case 3 -> GpuBackend.GLES109
else -> throw IllegalStateException(Some("invalid GPU backend"))110
}111
fun native(): NativeGpuDevice =112
when (backend()) {113
case Metal -> NativeGpuDevice.Metal(first!!, second!!)114
case D3D12 -> NativeGpuDevice.D3D12(first!!, second!!, third!!)115
case GLES -> NativeGpuDevice.GLES(first!!, second!!)116
}117
}119
@CLayout120
internal struct GpuTextureInfo(val pointer: Ptr<Unit>?, val name: UInt, val state: UInt) {121
fun native(backend: GpuBackend): NativeGpuTexture =122
when (backend) {123
case Metal -> NativeGpuTexture.Metal(pointer!!)124
case D3D12 -> NativeGpuTexture.D3D12(pointer!!, state)125
case GLES -> NativeGpuTexture.GLES(name)126
}127
}