scoop:scoop.graphics:0.1.0
src/drawing/shader.scoop
Selected production source · src/drawing/shader.scoop
1
package scoop.graphics3
/** A reusable, owner-thread SkSL shader program. */4
public class ShaderProgram private constructor(private val resource: GraphicsResource) : Closeable {5
public val isClosed: Boolean get() = resource.isClosed6
public override fun close() {7
resource.close()8
}10
public companion object {11
/** Compiles SkSL and reports the compiler's diagnostic on failure. */12
public fun compile(source: String): ShaderProgram {13
@Unsafe {14
val handle = source15
.withUtf8Bytes { bytes, length ->16
@Unsafe {17
return@withUtf8Bytes ownedGraphics(18
shaderCompile(bytes, length),19
"compile SkSL"20
)21
}22
}23
try {24
val error = shaderDiagnostic(handle.get()).message()25
when (error) {26
case Some(val message) -> throw GraphicsException(message)27
case None -> return ShaderProgram(handle)28
}29
} catch (error: Throwable) {30
handle.close()31
throw error32
}33
}34
}35
}37
/** Freezes this configuration as a shader for shape fills or another program's input. */38
public fun makeShader(configure: ShaderBindings.() -> Unit = {}): Shader = bind(-1, configure) {39
handle40
->41
@Unsafe {42
return@bind Shader(ownedGraphics(shaderMake(handle), "create shader"))43
}44
}46
internal fun filter(47
sourceChild: String,48
radius: Double,49
configure: ShaderBindings.() -> Unit50
): ImageFilter {51
checkScalar(radius)52
requireGraphics(radius >= 0.0, "negative shader sample radius")53
@Unsafe {54
val program = resource.get()55
val source = sourceChild56
.withUtf8Bytes { name, length ->57
@Unsafe {58
return@withUtf8Bytes shaderSource(program, name, length)59
}60
}61
requireGraphics(source >= 0, "unknown shader source: " + sourceChild)62
return bind(source, configure) { handle ->63
@Unsafe {64
return@bind ImageFilter(65
ownedGraphics(shaderFilter(handle, radius), "create shader filter")66
)67
}68
}69
}70
}72
private fun <T> bind(73
source: Int,74
configure: ShaderBindings.() -> Unit,75
finish: (Ptr<Unit>) -> T76
): T {77
@Unsafe {78
val handle = ownedGraphics(79
shaderBindings(resource.get(), source),80
"create shader bindings"81
)82
return handle83
.use { value ->84
ShaderBindings(value).configure()85
val pointer = value.get()86
@Unsafe {87
when (shaderMissing(pointer).message()) {88
case Some(val name) ->89
throw IllegalArgumentException(90
Some("missing shader input: " + name)91
)92
case None -> return@use finish(pointer)93
}94
}95
}96
}97
}98
}100
/** Immutable shader retaining its bound inputs. */101
public class Shader internal constructor(internal val resource: GraphicsResource) : Closeable {102
public val isClosed: Boolean get() = resource.isClosed103
public override fun close() {104
resource.close()105
}106
}108
@CLayout109
private struct ShaderText(val data: Ptr<UInt8>?, val length: Long) {110
fun message(): String? {111
@Unsafe {112
return when (data) {113
case Some(val bytes) -> Some(String.fromUtf8(bytes, length))114
case None -> None115
}116
}117
}118
}120
@Extern(name = "scoop_graphics_shader_compile")121
private fun shaderCompile(source: Ptr<UInt8>, length: Long): Ptr<Unit>?123
@Extern(name = "scoop_graphics_shader_diagnostic")124
private fun shaderDiagnostic(program: Ptr<Unit>): ShaderText126
@Extern(name = "scoop_graphics_shader_source")127
private fun shaderSource(program: Ptr<Unit>, name: Ptr<UInt8>, length: Long): Int129
@Extern(name = "scoop_graphics_shader_bindings")130
private fun shaderBindings(program: Ptr<Unit>, source: Int): Ptr<Unit>?132
@Extern(name = "scoop_graphics_shader_missing")133
private fun shaderMissing(bindings: Ptr<Unit>): ShaderText135
@Extern(name = "scoop_graphics_shader_make")136
private fun shaderMake(bindings: Ptr<Unit>): Ptr<Unit>?138
@Extern(name = "scoop_graphics_shader_filter")139
private fun shaderFilter(bindings: Ptr<Unit>, radius: Double): Ptr<Unit>?