Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ android {
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

shaders {
glslcArgs.addAll(["-O"])
}

externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions -Wall -Wextra -fstack-protector-all"
Expand Down Expand Up @@ -122,8 +118,6 @@ android {

sourceSets {
main {
shaders.srcDirs = ["src/main/shaders"]

if (isNewArchitectureEnabled()) {
java.srcDirs += [
// React Codegen files
Expand All @@ -134,6 +128,8 @@ android {
}
}

apply from: "./compile-shaders.gradle"

repositories {
mavenCentral()
google()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.process.ExecOperations

import javax.inject.Inject

@CacheableTask
abstract class CompileResizerShader extends DefaultTask {
// Include helper files in the input snapshot so edits to #includes recompile the shader.
@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
abstract DirectoryProperty getShaderDirectory()

@InputFile
@PathSensitive(PathSensitivity.NONE)
abstract RegularFileProperty getCompilerFile()

@OutputDirectory
abstract DirectoryProperty getOutputDirectory()

@Inject
abstract ExecOperations getExecOperations()

@TaskAction
void compile() {
def inputFile = shaderDirectory.file("Resizer.comp").get().asFile
def outputFile = outputDirectory.file("shaders/Resizer.comp.spv").get().asFile
outputFile.parentFile.mkdirs()
execOperations.exec {
commandLine compilerFile.get().asFile, "-O", inputFile, "-o", outputFile
}
}
}

// Own shader compilation on all AGP versions. AGP 9 disables its built-in task by
// default and requires a compiler path in the consuming app's local.properties.
android.buildFeatures.shaders = false

def osName = System.getProperty("os.name").toLowerCase(Locale.ROOT)
def hostTag
if (osName.contains("windows")) {
hostTag = "windows-x86_64"
} else if (osName.contains("mac") || osName.contains("darwin")) {
// The NDK uses this directory on both Intel and Apple Silicon Macs.
hostTag = "darwin-x86_64"
} else if (osName.contains("linux")) {
hostTag = "linux-x86_64"
} else {
throw new GradleException("Unsupported host OS for the VisionCamera Resizer shader compiler: ${osName}")
}

def compilerName = osName.contains("windows") ? "glslc.exe" : "glslc"
def shaderCompiler = androidComponents.sdkComponents.ndkDirectory.map { ndk ->
ndk.file("shader-tools/${hostTag}/${compilerName}")
}
def shaderSources = layout.projectDirectory.dir("src/main/shaders")

androidComponents.onVariants(androidComponents.selector().all()) { variant ->
def compileShader = tasks.register("compile${variant.name.capitalize()}ResizerShader", CompileResizerShader) {
shaderDirectory.set(shaderSources)
compilerFile.set(shaderCompiler)
}
// AGP supplies the output directory and makes asset merging depend on this task.
variant.sources.assets.addGeneratedSourceDirectory(compileShader) { it.outputDirectory }
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace {
throw std::runtime_error("Android asset manager is not available for Vulkan shader loading.");
}

// The shader is precompiled by AGP and packaged as an app asset, so native code only needs to read the SPIR-V bytes.
// The shader is precompiled by Gradle and packaged as an app asset, so native code only needs to read the SPIR-V bytes.
std::unique_ptr<AAsset, decltype(&AAsset_close)> asset(AAssetManager_open(assetManager, assetPath, AASSET_MODE_BUFFER), &AAsset_close);
if (asset == nullptr) [[unlikely]] {
throw std::runtime_error(std::string("Failed to open Vulkan shader asset `") + assetPath +
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-vision-camera-resizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"lib",
"nitrogen",
"android/build.gradle",
"android/compile-shaders.gradle",
"android/gradle.properties",
"android/fix-prefab.gradle",
"android/CMakeLists.txt",
Expand Down
Loading