Giter VIP home page Giter VIP logo

kotlin-glsl's Introduction

GLSL shaders development in Kotlin

The sources provide a way of creating GLSL-like code in Kotlin.

Shader source in Kotlin:

class FragmentShader(useAlphaTest: Boolean) : ShaderBuilder() {
    private val alphaTestThreshold by uniform(::GLFloat)
    private val texture by uniform(::Sampler2D)
    private val uv by varying(::Vec2)
    init {
        var color by vec4()
        color = texture2D(texture, uv)
        // static branching
        if (useAlphaTest) {
            // dynamic branching
            If(color.w lt alphaTestThreshold) {
                discard()
            }
        }
        gl_FragColor = color
    }
}

You can get your GLSL source in two ways:

1. Runtime generation

FragmentShader(useAlphaTest = true).getSource()

Pros: easy to generate sources.

Cons: GLSL swizzling functionality may affect performance, as it creates lots of objects at runtime.

2. Generation using @ShaderProgram annotation

This functionality can be used as follows:

@ShaderProgram(VertexShader::class, FragmentShader::class)
class ShaderProgramName(alphaTest: Boolean)

Annotation processor generates new sources during gradle kaptKotlin task which look as follows:

class ShaderProgramNameSources {
    enum class Sources(vertex: String, fragment: String): ShaderProgramSources {
        Source0("<vertex code>", "<fragment code>")
        ...
    }
    fun get(alphaTest: Boolean) {
        if (alphaTest) return Source0
        else return Source1
    }
}

After generation you can get both vertex and fragment sources as follows:

val sources = ShaderProgramNameSources.get(replaceAlpha = true)
println(sources.vertex)
println(sources.fragment)

Pros: no need to generate sources at runtime, as you will have direct access to them.

Cons: shader source classes have to be accessible for AP to load them, thus can't be stored just anywhere in the project.

Generated GLSL

Both methods produce the same result:

uniform sampler2D texture;
uniform float alphaTestThreshold;
varying vec2 uv;
void main(void) {
    vec4 color;
    color = texture2D(texture, uv);
    if ((color.w < alphaTestThreshold)) {
        discard;
    }
    gl_FragColor = color;
}

How to use

You will need gradle to build sources.

You have two options here. If you do not need @ShaderProgram annotation functionality in the above example, then you can simply import the KotlinGlsl project (and may remove ProgramExample module).

If you do want to use @ShaderProgram, then you will have to store your Kotlin shader sources either directly in the KotlinGlsl or in a project accessible for it during annotations processing stage, as AP will try to load classes provided with @ShaderProgram.

License

MIT

kotlin-glsl's People

Contributors

dananas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.