Skip to content

Instantly share code, notes, and snippets.

@Laxystem
Last active January 18, 2025 15:05
Show Gist options
  • Save Laxystem/17f943ba0e3e1ee32b6617756dd1b490 to your computer and use it in GitHub Desktop.
Save Laxystem/17f943ba0e3e1ee32b6617756dd1b490 to your computer and use it in GitHub Desktop.
Spock Shader API (Licensed under the MPL 2.0)
package quest.laxla.spock
/**
* Any kind of shader.
*
* @since 0.0.1-alpha.4
*/
public interface Shader {
/**
* The language this [Shader] is written in.
*
* @since 0.0.1-alpha.4
*/
public val language: Language
/**
* The label the rendering API should refer to this shader under for logging purposes.
*
* @since 0.0.1-alpha.4
*/
public val label: String? get() = null
/**
* Represents a shading language, such as [WGSL][Wgsl] or GLSL.
*
* Implementations **must** properly implement [equals] and [hashCode];
* Therefore, it is recommended to use `data object`s.
*
* @since 0.0.1-alpha.4
* @sample Wgsl
*/
public interface Language {
/**
* Does this [shader] carry all metadata required by this shading language?
*
* This function always returns `false` if [shader].[language] does not equal to `this` language.
*
* @since 0.0.1-alpha.4
* @see Shader.isCarryingRequiredMetadata
*/
public infix fun accepts(shader: Shader): Boolean
}
/**
* Implemented by [Shader]s dynamically compiled from another shader of a different [Language] or form factor.
*
* @param L the language this shader was transpiled to.
* @since 0.0.1-alpha.4
* @see Transpiler
*/
public interface Transpiled<L> : Shader where L : Language {
/**
* The original [Shader] this one was compiled from.
*
* @since 0.0.1-alpha.4
*/
public val original: Shader
override val language: L
}
/**
* Transpiles [Shader] from one [Language] or form factor to another if not supported by the [Renderer].
*
* @since 0.0.1-alpha.4
* @see Transpiled
*/
public interface Transpiler<L> where L : Language {
/**
* The [Language] the shaders this transpiler outputs are written in.
*
* @since 0.0.1-alpha.4
*/
public val outputLanguage: L
/**
* Transpiles the [input] [Shader] to the [outputLanguage].
*
* It is expected but not required that the output is [accept][Language.accepts]ed;
* However, if necessary, prefer returning an unaccepted shader over `null` when possible and not expensive.
*
* @return `null` if this [Transpiler] does not support this [input].
* @since 0.0.1-alpha.4
*/
public suspend operator fun invoke(input: Shader): Transpiled<L>?
}
}
package quest.laxla.spock
/**
* Does this [Shader] carry all metadata required by the [language][Shader.language] it is written in?
*
* @since 0.0.1-alpha.4
* @see Shader.Language.accepts
*/
public val Shader.isCarryingRequiredMetadata: Boolean get() = language accepts this
/**
* @author Laxystem
*/
private data class WgslVertexShader<Vertex : Any>(
override val vertexKind: VertexKind<Vertex>,
override val code: String,
override val entrypoint: String,
override val label: String?
) : VertexShader<Vertex>, Wgsl.Shader, StringShader
/**
* Creates a [Wgsl]&nbsp;[VertexShader] from a [String].
*
* @since 0.0.1-alpha.4
*/
public fun <Vertex : Any> wgslVertexShaderOf(
wgsl: String,
vertexKind: VertexKind<Vertex>,
entrypoint: String,
label: String? = null
): VertexShader<Vertex> = WgslVertexShader(vertexKind, wgsl, entrypoint, label)
package quest.laxla.spock
/**
* Non-compiled [Shader], that is passed as a [String][kotlin.String].
*
* @since 0.0.1-alpha.4
*/
public interface StringShader : Shader {
/**
* The shader's non-compiled code, written in [language].
*
* There's no performance penalty to different [Shader]s sharing the same code,
* as it is the [Renderer]'s responsibility to handle such situations.
*
* @since 0.0.1-alpha.4
*/
public val code: String
}
package quest.laxla.spock
import quest.laxla.spock.Shader as AnyShader
import quest.laxla.spock.Shader.Language as ShadingLanguage
/**
* The WGSL shading language, developed for WebGPU.
*
* @since 0.0.1-alpha.4
*/
public data object Wgsl : ShadingLanguage {
public interface Shader : AnyShader {
/**
* The name of the function in this shader to be called by the GPU.
*
* @since 0.0.1-alpha.4
*/
public val entrypoint: String
override val language: Wgsl get() = Wgsl
}
override fun accepts(shader: AnyShader): Boolean = shader is Wgsl.Shader
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment