diff --git a/skiko/src/commonMain/kotlin/org/jetbrains/skia/RuntimeShaderBuilder.kt b/skiko/src/commonMain/kotlin/org/jetbrains/skia/RuntimeShaderBuilder.kt index 373a73f55..6da02c1d0 100644 --- a/skiko/src/commonMain/kotlin/org/jetbrains/skia/RuntimeShaderBuilder.kt +++ b/skiko/src/commonMain/kotlin/org/jetbrains/skia/RuntimeShaderBuilder.kt @@ -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 { @@ -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) diff --git a/skiko/src/commonTest/kotlin/org/jetbrains/skia/RuntimeShaderBuilderTest.kt b/skiko/src/commonTest/kotlin/org/jetbrains/skia/RuntimeShaderBuilderTest.kt index 8576fa37a..ab3564446 100644 --- a/skiko/src/commonTest/kotlin/org/jetbrains/skia/RuntimeShaderBuilderTest.kt +++ b/skiko/src/commonTest/kotlin/org/jetbrains/skia/RuntimeShaderBuilderTest.kt @@ -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 = """ diff --git a/skiko/src/jvmMain/cpp/common/RuntimeShaderBuilder.cc b/skiko/src/jvmMain/cpp/common/RuntimeShaderBuilder.cc index 0bd49fe33..499998c28 100644 --- a/skiko/src/jvmMain/cpp/common/RuntimeShaderBuilder.cc +++ b/skiko/src/jvmMain/cpp/common/RuntimeShaderBuilder.cc @@ -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(builderPtr); + jfloat* floatArray = static_cast(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) { diff --git a/skiko/src/nativeJsMain/cpp/RuntimeShaderBuilder.cc b/skiko/src/nativeJsMain/cpp/RuntimeShaderBuilder.cc index bb7baab96..6c4df7b5b 100644 --- a/skiko/src/nativeJsMain/cpp/RuntimeShaderBuilder.cc +++ b/skiko/src/nativeJsMain/cpp/RuntimeShaderBuilder.cc @@ -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(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(builderPtr);