[orx-shader-phrases] Add ShaderPhraseRegistry
This commit is contained in:
3
orx-shader-phrases/build.gradle
Normal file
3
orx-shader-phrases/build.gradle
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
dependencies {
|
||||||
|
api "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
|
||||||
|
}
|
||||||
@@ -1,9 +1,81 @@
|
|||||||
package org.openrndr.extra.shaderphrases
|
package org.openrndr.extra.shaderphrases
|
||||||
|
|
||||||
|
import mu.KotlinLogging
|
||||||
import org.openrndr.draw.Shader
|
import org.openrndr.draw.Shader
|
||||||
import org.openrndr.draw.codeFromURL
|
import org.openrndr.draw.codeFromURL
|
||||||
import org.openrndr.extra.shaderphrases.annotations.ShaderPhrases
|
import org.openrndr.extra.shaderphrases.annotations.ShaderPhrases
|
||||||
import org.openrndr.internal.Driver
|
import kotlin.reflect.KProperty1
|
||||||
|
import kotlin.reflect.full.declaredMemberProperties
|
||||||
|
|
||||||
|
private val logger = KotlinLogging.logger {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A single shader phrase.
|
||||||
|
*/
|
||||||
|
class ShaderPhrase(val phrase: String) {
|
||||||
|
/**
|
||||||
|
* Register this shader phrase in the [ShaderPhraseRegistry]
|
||||||
|
* This will likely be called by [ShaderPhraseBook]
|
||||||
|
*/
|
||||||
|
fun register(bookId: String? = null) {
|
||||||
|
val functionRex =
|
||||||
|
Regex("(float|int|[bi]?vec2|[bi]?vec3|[bi]?vec4|mat3|mat4)[ ]+([a-zA-Z0-9_]+)[ ]*\\(.*\\).*")
|
||||||
|
val defs = phrase.split("\n").filter {
|
||||||
|
functionRex.matches(it)
|
||||||
|
}.take(1).mapNotNull {
|
||||||
|
val m = functionRex.find(it)
|
||||||
|
m?.groupValues?.getOrNull(2)
|
||||||
|
}
|
||||||
|
val id = defs.firstOrNull() ?: error("no function body found in phrase")
|
||||||
|
ShaderPhraseRegistry.registerPhrase("${bookId?.let { "$it." } ?: ""}$id", this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A book of shader phrases.
|
||||||
|
*/
|
||||||
|
open class ShaderPhraseBook(val bookId: String) {
|
||||||
|
private var registered = false
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers all known shader phrases
|
||||||
|
*/
|
||||||
|
fun register() {
|
||||||
|
if (!registered) {
|
||||||
|
this::class.declaredMemberProperties.filter {
|
||||||
|
it.returnType.toString() == "org.openrndr.extra.shaderphrases.ShaderPhrase"
|
||||||
|
}.map {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
val m = it as? KProperty1<ShaderPhraseBook, ShaderPhrase>
|
||||||
|
m?.get(this)?.register(bookId)
|
||||||
|
}
|
||||||
|
registered = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The global, application-wide, shader phrase registry
|
||||||
|
*/
|
||||||
|
object ShaderPhraseRegistry {
|
||||||
|
private val phrases = mutableMapOf<String, ShaderPhrase>()
|
||||||
|
/**
|
||||||
|
* Registers a [phrase] with [id]
|
||||||
|
*/
|
||||||
|
fun registerPhrase(id: String, phrase: ShaderPhrase) {
|
||||||
|
phrases[id] = phrase
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a phrase for [id], returns null when no phrase found
|
||||||
|
*/
|
||||||
|
fun findPhrase(id: String): ShaderPhrase? {
|
||||||
|
val phrase = phrases[id]
|
||||||
|
if (phrase == null) {
|
||||||
|
logger.warn { "no phrase found for id: \"$id\"" }
|
||||||
|
}
|
||||||
|
return phrase
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Preprocess shader source.
|
* Preprocess shader source.
|
||||||
@@ -27,38 +99,46 @@ fun preprocessShader(source: String, symbols: Set<String> = emptySet()): String
|
|||||||
|
|
||||||
if (symbol !in newSymbols) {
|
if (symbol !in newSymbols) {
|
||||||
newSymbols.add(symbol)
|
newSymbols.add(symbol)
|
||||||
try {
|
val registryPhrase = ShaderPhraseRegistry.findPhrase(symbol)
|
||||||
/* Note that JVM-style reflection is used here because of short-comings in the Kotlin reflection
|
|
||||||
library (as of 1.3.61), most notably reflection support for file facades is missing. */
|
registryPhrase?.let { preprocessShader(it.phrase, newSymbols) }
|
||||||
val c = Class.forName(packageClass)
|
?: try {
|
||||||
if (c.annotations.any { it.annotationClass == ShaderPhrases::class }) {
|
/* Note that JVM-style reflection is used here because of short-comings in the Kotlin reflection
|
||||||
if (fieldName == "*") {
|
library (as of 1.3.61), most notably reflection support for file facades is missing. */
|
||||||
c.declaredMethods.filter { it.returnType.name == "java.lang.String" }.map {
|
val c = Class.forName(packageClass)
|
||||||
"/* imported from $packageClass.$it */\n${it.invoke(null)}\n"
|
if (c.annotations.any { it.annotationClass == ShaderPhrases::class }) {
|
||||||
}.joinToString("\n") +
|
if (fieldName == "*") {
|
||||||
c.declaredFields.filter { it.type.name == "java.lang.String" }.map {
|
c.declaredMethods.filter { it.returnType.name == "java.lang.String" }.map {
|
||||||
"/* imported from $packageClass.$it */\n${it.get(null)}\n"
|
"/* imported from $packageClass.$it */\n${it.invoke(null)}\n"
|
||||||
}.joinToString("\n")
|
}.joinToString("\n") +
|
||||||
} else {
|
c.declaredFields.filter { it.type.name == "java.lang.String" }.map {
|
||||||
var result: String?
|
"/* imported from $packageClass.$it */\n${it.get(null)}\n"
|
||||||
try {
|
}.joinToString("\n")
|
||||||
val methodName = "get${fieldName.take(1).toUpperCase() + fieldName.drop(1)}"
|
} else {
|
||||||
result = preprocessShader(c.getMethod(methodName).invoke(null) as String, newSymbols)
|
var result: String?
|
||||||
} catch (e: NoSuchMethodException) {
|
|
||||||
try {
|
try {
|
||||||
result = preprocessShader(c.getDeclaredField(fieldName).get(null) as String, newSymbols)
|
val methodName = "get${fieldName.take(1).toUpperCase() + fieldName.drop(1)}"
|
||||||
} catch (e: NoSuchFieldException) {
|
result =
|
||||||
error("field \"$fieldName\" not found in \"#pragma import $packageClass.$fieldName\" on line ${index + 1}")
|
preprocessShader(c.getMethod(methodName).invoke(null) as String, newSymbols)
|
||||||
|
} catch (e: NoSuchMethodException) {
|
||||||
|
try {
|
||||||
|
result =
|
||||||
|
preprocessShader(
|
||||||
|
c.getDeclaredField(fieldName).get(null) as String,
|
||||||
|
newSymbols
|
||||||
|
)
|
||||||
|
} catch (e: NoSuchFieldException) {
|
||||||
|
error("field \"$fieldName\" not found in \"#pragma import $packageClass.$fieldName\" on line ${index + 1}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
result
|
||||||
}
|
}
|
||||||
result
|
} else {
|
||||||
|
throw IllegalArgumentException("class $packageClass has no ShaderPhrases annotation")
|
||||||
}
|
}
|
||||||
} else {
|
} catch (e: ClassNotFoundException) {
|
||||||
throw IllegalArgumentException("class $packageClass has no ShaderPhrases annotation")
|
error("class \"$packageClass\" not found in \"#pragma import $packageClass\" on line ${index + 1}")
|
||||||
}
|
}
|
||||||
} catch (e: ClassNotFoundException) {
|
|
||||||
error("class \"$packageClass\" not found in \"#pragma import $packageClass\" on line ${index + 1}")
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
}
|
}
|
||||||
@@ -83,11 +163,12 @@ fun preprocessShaderFromUrl(url: String, symbols: Set<String> = emptySet()): Str
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun Shader.Companion.preprocessedFromUrls(
|
fun Shader.Companion.preprocessedFromUrls(
|
||||||
vsUrl: String,
|
vsUrl: String,
|
||||||
tcsUrl: String? = null,
|
tcsUrl: String? = null,
|
||||||
tesUrl: String? = null,
|
tesUrl: String? = null,
|
||||||
gsUrl: String? = null,
|
gsUrl: String? = null,
|
||||||
fsUrl: String): Shader {
|
fsUrl: String
|
||||||
|
): Shader {
|
||||||
|
|
||||||
val vsCode = codeFromURL(vsUrl).preprocess()
|
val vsCode = codeFromURL(vsUrl).preprocess()
|
||||||
val tcsCode = tcsUrl?.let { codeFromURL(it) }?.preprocess()
|
val tcsCode = tcsUrl?.let { codeFromURL(it) }?.preprocess()
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ object TestPreprocessShader : Spek({
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("A shader with import statements") {
|
describe("A shader with import statements") {
|
||||||
val shader = """#version 330
|
val shader = """
|
||||||
#pragma import org.openrndr.extra.shaderphrases.phrases.Dummy.*
|
|#version 330
|
||||||
"""
|
|#pragma import org.openrndr.extra.shaderphrases.phrases.Dummy.*""".trimMargin()
|
||||||
describe("injects dummy phrase when preprocessed") {
|
describe("injects dummy phrase when preprocessed") {
|
||||||
val processed = preprocessShader(shader)
|
val processed = preprocessShader(shader)
|
||||||
processed `should contain` "float dummy"
|
processed `should contain` "float dummy"
|
||||||
|
|||||||
20
orx-shader-phrases/src/test/kotlin/TestShaderPhrase.kt
Normal file
20
orx-shader-phrases/src/test/kotlin/TestShaderPhrase.kt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import org.amshove.kluent.`should be`
|
||||||
|
import org.openrndr.extra.shaderphrases.ShaderPhrase
|
||||||
|
import org.openrndr.extra.shaderphrases.ShaderPhraseRegistry
|
||||||
|
import org.spekframework.spek2.Spek
|
||||||
|
import org.spekframework.spek2.style.specification.describe
|
||||||
|
|
||||||
|
object TestShaderPhrase : Spek({
|
||||||
|
describe("A shader phrase") {
|
||||||
|
val phrase = ShaderPhrase("""
|
||||||
|
|vec4 test_phrase() {
|
||||||
|
|}
|
||||||
|
""".trimMargin() )
|
||||||
|
it("can be registered") {
|
||||||
|
phrase.register()
|
||||||
|
}
|
||||||
|
it("can be found") {
|
||||||
|
ShaderPhraseRegistry.findPhrase("test_phrase") `should be` phrase
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
23
orx-shader-phrases/src/test/kotlin/TestShaderPhraseBook.kt
Normal file
23
orx-shader-phrases/src/test/kotlin/TestShaderPhraseBook.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import org.amshove.kluent.`should be`
|
||||||
|
import org.openrndr.extra.shaderphrases.ShaderPhrase
|
||||||
|
import org.openrndr.extra.shaderphrases.ShaderPhraseBook
|
||||||
|
import org.openrndr.extra.shaderphrases.ShaderPhraseRegistry
|
||||||
|
import org.spekframework.spek2.Spek
|
||||||
|
import org.spekframework.spek2.style.specification.describe
|
||||||
|
|
||||||
|
class TestShaderPhraseBookobject : Spek({
|
||||||
|
describe("A shader phrase book") {
|
||||||
|
val book = object:ShaderPhraseBook("testBook") {
|
||||||
|
val phrase = ShaderPhrase("""
|
||||||
|
|vec4 test_phrase() {
|
||||||
|
|}
|
||||||
|
""".trimMargin() )
|
||||||
|
}
|
||||||
|
it("can be registered") {
|
||||||
|
book.register()
|
||||||
|
}
|
||||||
|
it("can be found") {
|
||||||
|
ShaderPhraseRegistry.findPhrase("testBook.test_phrase") `should be` book.phrase
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user