Skip to content

Commit

Permalink
RuntimeShaderBuilder uniform float array api (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
sphrak authored Aug 14, 2024
1 parent f34519d commit dc80eb4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ class RuntimeShaderBuilder internal constructor(ptr: NativePointer) : Managed(pt
}
}

fun uniform(name: String, value: FloatArray) {
Stats.onNativeCall()
interopScope {
_nUniformFloatArray(_ptr, toInterop(name), toInterop(value), value.size)
}
}


fun uniform(name: String, value: Matrix22) {
Stats.onNativeCall()
interopScope {
Expand Down Expand Up @@ -177,6 +185,10 @@ private external fun _nUniformFloat3(builderPtr: NativePointer, uniformName: Int
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_RuntimeShaderBuilder__1nUniformFloat4")
private external fun _nUniformFloat4(builderPtr: NativePointer, uniformName: InteropPointer, uniformValue1: Float, uniformValue2: Float, uniformValue3: Float, uniformValue4: Float)

@ExternalSymbolName("org_jetbrains_skia_RuntimeShaderBuilder__1nUniformFloatArray")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_RuntimeShaderBuilder__1nUniformFloatArray")
private external fun _nUniformFloatArray(builderPtr: NativePointer, uniformName: InteropPointer, uniformFloatArray: InteropPointer, length: Int)

@ExternalSymbolName("org_jetbrains_skia_RuntimeShaderBuilder__1nUniformFloatMatrix22")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_RuntimeShaderBuilder__1nUniformFloatMatrix22")
private external fun _nUniformFloatMatrix22(builderPtr: NativePointer, uniformName: InteropPointer, uniformMatrix22: InteropPointer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ class RuntimeShaderBuilderTest {
runtimeShaderBuilder.makeShader()
}

@Test
fun customShaderWithFloatArray() = shaderTest {
val shaderSksl = """
uniform float array[4];
half4 main(vec2 coord) {
return half4(array[0], array[1], array[2], array[3]);
}
""".trimIndent()

val runtimeEffect = RuntimeEffect.makeForShader(shaderSksl)
val runtimeShaderBuilder = RuntimeShaderBuilder(runtimeEffect)
runtimeShaderBuilder.uniform("array", floatArrayOf(0.1f, 0.2f, 0.3f, 0.4f))
runtimeShaderBuilder.makeShader()
}

@Test
fun customShaderWithMatrix() = shaderTest {
val shaderSksl = """
Expand Down
9 changes: 9 additions & 0 deletions skiko/src/jvmMain/cpp/common/RuntimeShaderBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ Java_org_jetbrains_skia_RuntimeShaderBuilderKt__1nUniformFloat4
runtimeShaderBuilder->uniform(skString(env, uniformName).c_str()) = float4 {uniformValue1, uniformValue2, uniformValue3, uniformValue4};
}

extern "C" JNIEXPORT void JNICALL
Java_org_jetbrains_skia_RuntimeShaderBuilderKt__1nUniformFloatArray
(JNIEnv* env, jclass jclass, jlong builderPtr, jstring uniformName, jfloatArray uniformFloatArray, jint length) {
SkRuntimeShaderBuilder* runtimeShaderBuilder = jlongToPtr<SkRuntimeShaderBuilder*>(builderPtr);
jfloat* floatArray = static_cast<jfloat*>(env->GetPrimitiveArrayCritical(uniformFloatArray, 0));
runtimeShaderBuilder->uniform(skString(env, uniformName).c_str()).set(floatArray, length);
env->ReleasePrimitiveArrayCritical(uniformFloatArray, floatArray, 0);
}

extern "C" JNIEXPORT void JNICALL
Java_org_jetbrains_skia_RuntimeShaderBuilderKt__1nUniformFloatMatrix22
(JNIEnv* env, jclass jclass, jlong builderPtr, jstring uniformName, jfloatArray uniformMatrix22) {
Expand Down
6 changes: 6 additions & 0 deletions skiko/src/nativeJsMain/cpp/RuntimeShaderBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ SKIKO_EXPORT void org_jetbrains_skia_RuntimeShaderBuilder__1nUniformFloat4
runtimeShaderBuilder->uniform(skString(uniformName).c_str()) = float4 {uniformValue1, uniformValue2, uniformValue3, uniformValue4};
}

SKIKO_EXPORT void org_jetbrains_skia_RuntimeShaderBuilder__1nUniformFloatArray
(KNativePointer builderPtr, KInteropPointer uniformName, KFloat* uniformFloatArray, int32_t length) {
SkRuntimeShaderBuilder* runtimeShaderBuilder = reinterpret_cast<SkRuntimeShaderBuilder*>(builderPtr);
runtimeShaderBuilder->uniform(skString(uniformName).c_str()).set(uniformFloatArray, length);
}

SKIKO_EXPORT void org_jetbrains_skia_RuntimeShaderBuilder__1nUniformFloatMatrix22
(KNativePointer builderPtr, KInteropPointer uniformName, KFloat* uniformMatrix22) {
SkRuntimeShaderBuilder* runtimeShaderBuilder = reinterpret_cast<SkRuntimeShaderBuilder*>(builderPtr);
Expand Down

0 comments on commit dc80eb4

Please sign in to comment.