|
| 1 | +// This plugin accepts the following parameters: |
| 2 | +// |
| 3 | +// com.google.android.filament.tools-dir |
| 4 | +// Path to the Filament distribution/install directory for desktop. |
| 5 | +// This directory must contain bin/matc. |
| 6 | +// |
| 7 | +// com.google.android.filament.exclude-vulkan |
| 8 | +// When set, support for Vulkan will be excluded. |
| 9 | +// |
| 10 | +// Example: |
| 11 | +// ./gradlew -Pcom.google.android.filament.tools-dir=../../dist-release assembleDebug |
| 12 | + |
| 13 | +import org.gradle.api.DefaultTask |
| 14 | +import org.gradle.api.GradleException |
| 15 | +import org.gradle.api.Plugin |
| 16 | +import org.gradle.api.Project |
| 17 | +import org.gradle.api.file.DirectoryProperty |
| 18 | +import org.gradle.api.file.FileSystemOperations |
| 19 | +import org.gradle.api.file.FileType |
| 20 | +import org.gradle.api.file.RegularFileProperty |
| 21 | +import org.gradle.api.logging.LogLevel |
| 22 | +import org.gradle.api.logging.Logger |
| 23 | +import org.gradle.api.model.ObjectFactory |
| 24 | +import org.gradle.api.provider.Property |
| 25 | +import org.gradle.api.provider.ProviderFactory |
| 26 | +import org.gradle.api.tasks.* |
| 27 | +import org.gradle.api.tasks.incremental.InputFileDetails |
| 28 | +import org.gradle.internal.os.OperatingSystem |
| 29 | +import org.gradle.process.ExecOperations |
| 30 | +import org.gradle.work.ChangeType |
| 31 | +import org.gradle.work.Incremental |
| 32 | +import org.gradle.work.InputChanges |
| 33 | + |
| 34 | +import javax.inject.Inject |
| 35 | +import java.nio.file.Paths |
| 36 | + |
| 37 | +abstract class TaskWithBinary extends DefaultTask { |
| 38 | + private final String binaryName |
| 39 | + private Property<String> binaryPath = null |
| 40 | + |
| 41 | + TaskWithBinary(String name) { |
| 42 | + binaryName = name |
| 43 | + } |
| 44 | + |
| 45 | + @Inject abstract ObjectFactory getObjects() |
| 46 | + @Inject abstract ProviderFactory getProviders() |
| 47 | + |
| 48 | + @Input |
| 49 | + Property<String> getBinary() { |
| 50 | + if (binaryPath == null) { |
| 51 | + def tool = ["/bin/${binaryName}.exe", "/bin/${binaryName}"] |
| 52 | + def fullPath = tool.collect { path -> |
| 53 | + def filamentToolsPath = providers |
| 54 | + .gradleProperty("com.google.android.filament.tools-dir") |
| 55 | + .forUseAtConfigurationTime().get() |
| 56 | + def directory = objects.fileProperty() |
| 57 | + .fileValue(new File(filamentToolsPath)).getAsFile().get() |
| 58 | + Paths.get(directory.absolutePath, path).toFile() |
| 59 | + } |
| 60 | + |
| 61 | + binaryPath = objects.property(String.class) |
| 62 | + binaryPath.set( |
| 63 | + (OperatingSystem.current().isWindows() ? fullPath[0] : fullPath[1]).toString()) |
| 64 | + } |
| 65 | + return binaryPath |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class LogOutputStream extends ByteArrayOutputStream { |
| 70 | + private final Logger logger |
| 71 | + private final LogLevel level |
| 72 | + |
| 73 | + LogOutputStream(Logger logger, LogLevel level) { |
| 74 | + this.logger = logger |
| 75 | + this.level = level |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + void flush() { |
| 80 | + logger.log(level, toString()) |
| 81 | + reset() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Custom task to compile material files using matc |
| 86 | +// This task handles incremental builds |
| 87 | +abstract class MaterialCompiler extends TaskWithBinary { |
| 88 | + @Incremental |
| 89 | + @InputDirectory |
| 90 | + abstract DirectoryProperty getInputDir() |
| 91 | + |
| 92 | + @OutputDirectory |
| 93 | + abstract DirectoryProperty getOutputDir() |
| 94 | + |
| 95 | + @Inject abstract FileSystemOperations getFs() |
| 96 | + @Inject abstract ExecOperations getExec() |
| 97 | + @Inject abstract ObjectFactory getObjects() |
| 98 | + @Inject abstract ProviderFactory getProviders() |
| 99 | + |
| 100 | + MaterialCompiler() { |
| 101 | + super("matc") |
| 102 | + } |
| 103 | + |
| 104 | + @TaskAction |
| 105 | + void execute(InputChanges inputs) { |
| 106 | + if (!inputs.incremental) { |
| 107 | + fs.delete({ |
| 108 | + delete(objects.fileTree().from(outputDir).matching { include '*.filamat' }) |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + inputs.getFileChanges(inputDir).each { InputFileDetails change -> |
| 113 | + if (change.fileType == FileType.DIRECTORY) return |
| 114 | + |
| 115 | + def file = change.file |
| 116 | + |
| 117 | + if (change.changeType == ChangeType.REMOVED) { |
| 118 | + getOutputFile(file).delete() |
| 119 | + } else { |
| 120 | + def out = new LogOutputStream(logger, LogLevel.LIFECYCLE) |
| 121 | + def err = new LogOutputStream(logger, LogLevel.ERROR) |
| 122 | + |
| 123 | + def header = ("Compiling material " + file + "\n").getBytes() |
| 124 | + out.write(header) |
| 125 | + out.flush() |
| 126 | + |
| 127 | + if (!new File(binary.get()).exists()) { |
| 128 | + throw new GradleException("Could not find ${binary.get()}." + |
| 129 | + " Ensure Filament has been built/installed before building this app.") |
| 130 | + } |
| 131 | + |
| 132 | + def matcArgs = [] |
| 133 | + def exclude_vulkan = providers |
| 134 | + .gradleProperty("com.google.android.filament.exclude-vulkan") |
| 135 | + .forUseAtConfigurationTime().present |
| 136 | + if (!exclude_vulkan) { |
| 137 | + matcArgs += ['-a', 'vulkan'] |
| 138 | + } |
| 139 | + matcArgs += ['-a', 'opengl', '-p', 'mobile', '-o', getOutputFile(file), file] |
| 140 | + |
| 141 | + exec.exec { |
| 142 | + standardOutput out |
| 143 | + errorOutput err |
| 144 | + executable "${binary.get()}" |
| 145 | + args matcArgs |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + File getOutputFile(final File file) { |
| 152 | + return outputDir.file(file.name[0..file.name.lastIndexOf('.')] + 'filamat').get().asFile |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// Custom task to process IBLs using cmgen |
| 157 | +// This task handles incremental builds |
| 158 | +abstract class IblGenerator extends TaskWithBinary { |
| 159 | + @Input |
| 160 | + @Optional |
| 161 | + abstract Property<String> getCmgenArgs() |
| 162 | + |
| 163 | + @Input |
| 164 | + @Optional |
| 165 | + abstract Property<String> getFormat() |
| 166 | + |
| 167 | + @Incremental |
| 168 | + @InputDirectory |
| 169 | + abstract DirectoryProperty getInputDir() |
| 170 | + |
| 171 | + @OutputDirectory |
| 172 | + abstract DirectoryProperty getOutputDir() |
| 173 | + |
| 174 | + @Inject abstract FileSystemOperations getFs() |
| 175 | + @Inject abstract ExecOperations getExec() |
| 176 | + @Inject abstract ObjectFactory getObjects() |
| 177 | + |
| 178 | + IblGenerator() { |
| 179 | + super("cmgen") |
| 180 | + } |
| 181 | + |
| 182 | + @TaskAction |
| 183 | + void execute(InputChanges inputs) { |
| 184 | + if (!inputs.incremental) { |
| 185 | + fs.delete({ |
| 186 | + delete(objects.fileTree().from(outputDir).matching { include '*' }) |
| 187 | + }) |
| 188 | + } |
| 189 | + |
| 190 | + inputs.getFileChanges(inputDir).each { InputFileDetails change -> |
| 191 | + if (change.fileType == FileType.DIRECTORY) return |
| 192 | + |
| 193 | + def file = change.file |
| 194 | + |
| 195 | + if (change.changeType == ChangeType.REMOVED) { |
| 196 | + getOutputFile(file).delete() |
| 197 | + } else { |
| 198 | + def out = new LogOutputStream(logger, LogLevel.LIFECYCLE) |
| 199 | + def err = new LogOutputStream(logger, LogLevel.ERROR) |
| 200 | + |
| 201 | + def header = ("Generating IBL " + file + "\n").getBytes() |
| 202 | + out.write(header) |
| 203 | + out.flush() |
| 204 | + |
| 205 | + if (!new File(binary.get()).exists()) { |
| 206 | + throw new GradleException("Could not find ${binary.get()}." + |
| 207 | + " Ensure Filament has been built/installed before building this app.") |
| 208 | + } |
| 209 | + |
| 210 | + def outputPath = getOutputFile(file)//outputDir.get().asFile |
| 211 | + def commandArgs = cmgenArgs.getOrNull() |
| 212 | + if (commandArgs == null) { |
| 213 | + def format = format.getOrElse("rgb32f") |
| 214 | + commandArgs = |
| 215 | + '-q -x ' + outputPath + ' --format=' + format + |
| 216 | + ' --extract-blur=0.08 --extract=' + outputPath.absolutePath |
| 217 | + } |
| 218 | + commandArgs = commandArgs + " " + file |
| 219 | + |
| 220 | + exec.exec { |
| 221 | + standardOutput out |
| 222 | + errorOutput err |
| 223 | + executable "${binary.get()}" |
| 224 | + args(commandArgs.split()) |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + File getOutputFile(final File file) { |
| 231 | + return outputDir.file(file.name[0..file.name.lastIndexOf('.') - 1]).get().asFile |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +// Custom task to compile mesh files using filamesh |
| 236 | +// This task handles incremental builds |
| 237 | +abstract class MeshCompiler extends TaskWithBinary { |
| 238 | + @Incremental |
| 239 | + @InputFile |
| 240 | + abstract RegularFileProperty getInputFile() |
| 241 | + |
| 242 | + @OutputDirectory |
| 243 | + abstract DirectoryProperty getOutputDir() |
| 244 | + |
| 245 | + @Inject abstract FileSystemOperations getFs() |
| 246 | + @Inject abstract ExecOperations getExec() |
| 247 | + |
| 248 | + MeshCompiler() { |
| 249 | + super("filamesh") |
| 250 | + } |
| 251 | + |
| 252 | + @TaskAction |
| 253 | + void execute(InputChanges inputs) { |
| 254 | + if (!inputs.incremental) { |
| 255 | + fs.delete({ |
| 256 | + delete(objects.fileTree().from(outputDir).matching { include '*.filamesh' }) |
| 257 | + }) |
| 258 | + } |
| 259 | + |
| 260 | + inputs.getFileChanges(inputFile).each { InputFileDetails change -> |
| 261 | + if (change.fileType == FileType.DIRECTORY) return |
| 262 | + |
| 263 | + def file = change.file |
| 264 | + |
| 265 | + if (change.changeType == ChangeType.REMOVED) { |
| 266 | + getOutputFile(file).delete() |
| 267 | + } else { |
| 268 | + def out = new LogOutputStream(logger, LogLevel.LIFECYCLE) |
| 269 | + def err = new LogOutputStream(logger, LogLevel.ERROR) |
| 270 | + |
| 271 | + def header = ("Compiling mesh " + file + "\n").getBytes() |
| 272 | + out.write(header) |
| 273 | + out.flush() |
| 274 | + |
| 275 | + if (!new File(binary.get()).exists()) { |
| 276 | + throw new GradleException("Could not find ${binary.get()}." + |
| 277 | + " Ensure Filament has been built/installed before building this app.") |
| 278 | + } |
| 279 | + |
| 280 | + exec.exec { |
| 281 | + standardOutput out |
| 282 | + errorOutput err |
| 283 | + executable "${binary.get()}" |
| 284 | + args(file, getOutputFile(file)) |
| 285 | + } |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + File getOutputFile(final File file) { |
| 291 | + return outputDir.file(file.name[0..file.name.lastIndexOf('.')] + 'filamesh').get().asFile |
| 292 | + } |
| 293 | +} |
| 294 | + |
| 295 | +class FilamentToolsPluginExtension { |
| 296 | + DirectoryProperty materialInputDir |
| 297 | + DirectoryProperty materialOutputDir |
| 298 | + |
| 299 | + String cmgenArgs |
| 300 | + DirectoryProperty iblInputDir |
| 301 | + DirectoryProperty iblOutputDir |
| 302 | + String iblFormat |
| 303 | + |
| 304 | + RegularFileProperty meshInputFile |
| 305 | + DirectoryProperty meshOutputDir |
| 306 | +} |
| 307 | + |
| 308 | +class FilamentToolsPlugin implements Plugin<Project> { |
| 309 | + void apply(Project project) { |
| 310 | + def extension = project.extensions.create('filamentTools', FilamentToolsPluginExtension) |
| 311 | + extension.materialInputDir = project.objects.directoryProperty() |
| 312 | + extension.materialOutputDir = project.objects.directoryProperty() |
| 313 | + extension.iblInputDir = project.objects.directoryProperty() |
| 314 | + extension.iblOutputDir = project.objects.directoryProperty() |
| 315 | + extension.meshInputFile = project.objects.fileProperty() |
| 316 | + extension.meshOutputDir = project.objects.directoryProperty() |
| 317 | + |
| 318 | + project.tasks.register("filamentCompileMaterials", MaterialCompiler) { |
| 319 | + enabled = |
| 320 | + extension.materialInputDir.isPresent() && |
| 321 | + extension.materialOutputDir.isPresent() |
| 322 | + inputDir.set(extension.materialInputDir.getOrNull()) |
| 323 | + outputDir.set(extension.materialOutputDir.getOrNull()) |
| 324 | + } |
| 325 | + |
| 326 | + project.preBuild.dependsOn "filamentCompileMaterials" |
| 327 | + |
| 328 | + project.tasks.register("filamentGenerateIbl", IblGenerator) { |
| 329 | + enabled = extension.iblInputDir.isPresent() && extension.iblOutputDir.isPresent() |
| 330 | + cmgenArgs = extension.cmgenArgs |
| 331 | + inputDir = extension.iblInputDir.getOrNull() |
| 332 | + outputDir = extension.iblOutputDir.getOrNull() |
| 333 | + format = extension.iblFormat |
| 334 | + } |
| 335 | + |
| 336 | + project.preBuild.dependsOn "filamentGenerateIbl" |
| 337 | + |
| 338 | + project.tasks.register("filamentCompileMesh", MeshCompiler) { |
| 339 | + enabled = extension.meshInputFile.isPresent() && extension.meshOutputDir.isPresent() |
| 340 | + inputFile = extension.meshInputFile.getOrNull() |
| 341 | + outputDir = extension.meshOutputDir.getOrNull() |
| 342 | + } |
| 343 | + |
| 344 | + project.preBuild.dependsOn "filamentCompileMesh" |
| 345 | + } |
| 346 | +} |
0 commit comments