Scoop API

scoop:scoop.graphics:0.1.0

src/native/gpu_types.scoop

Selected production source · src/native/gpu_types.scoop

1package scoop.graphics.native
3public enum GpuBackend {
4 Metal, D3D12, GLES
5}
7/** The platform's unmodified pixel format; no format conversion or fallback is performed. */
8public enum NativeGpuFormat {
9 Metal(ULong), D3D12(UInt), GLES(UInt)
12/** How Skia interprets the texture's channels, independently of its native storage format. */
13public enum GpuPixelLayout {
14 Rgba8, Bgra8, Rgba16F, Rgb10A2, R8, Rg8
17public enum GpuColorSpace {
18 Srgb, LinearSrgb
21public enum GpuImageAlpha {
22 Premultiplied, Unpremultiplied, Opaque
25public enum GpuImageOrigin {
26 TopLeft, BottomLeft
29/** Borrowed device and queue, or EGL display and current context. Native owners use platform retain rules. */
30public enum NativeGpuDevice {
31 Metal(Ptr<Unit>, Ptr<Unit>), D3D12(Ptr<Unit>, Ptr<Unit>, Ptr<Unit>),
32 GLES(Ptr<Unit>, Ptr<Unit>)
35/** D3D12 includes the state which a native writer must restore before returning. */
36public enum NativeGpuTexture {
37 Metal(Ptr<Unit>), D3D12(Ptr<Unit>, UInt), GLES(UInt)
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@CLayout
46public struct NativeGpuImageOwner(
47 val payload: Ptr<Unit>,
48 val retain: GCLeafFunPtr<(Ptr<Unit>) -> Unit>,
49 val release: GCLeafFunPtr<(Ptr<Unit>, UInt) -> Unit>
52internal fun NativeGpuFormat.backend(): GpuBackend =
53 when (this) {
54 case Metal(_) -> GpuBackend.Metal
55 case D3D12(_) -> GpuBackend.D3D12
56 case GLES(_) -> GpuBackend.GLES
57 }
59internal fun NativeGpuFormat.code(): ULong =
60 when (this) {
61 case Metal(val value) -> value
62 case D3D12(val value) -> value.toULong()
63 case GLES(val value) -> value.toULong()
64 }
66internal fun GpuPixelLayout.code(): Int =
67 when (this) {
68 case Rgba8 -> 0
69 case Bgra8 -> 1
70 case Rgba16F -> 2
71 case Rgb10A2 -> 3
72 case R8 -> 4
73 case Rg8 -> 5
74 }
76internal fun GpuImageAlpha.code(): Int =
77 when (this) {
78 case Premultiplied -> 0
79 case Unpremultiplied -> 1
80 case Opaque -> 2
81 }
83internal fun NativeGpuTexture.backend(): GpuBackend =
84 when (this) {
85 case Metal(_) -> GpuBackend.Metal
86 case D3D12(_, _) -> GpuBackend.D3D12
87 case GLES(_) -> GpuBackend.GLES
88 }
90internal 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@CLayout
98internal 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.Metal
107 case 2 -> GpuBackend.D3D12
108 case 3 -> GpuBackend.GLES
109 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 }
119@CLayout
120internal 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 }