Coder Social home page Coder Social logo

Comments (1)

tkhskt avatar tkhskt commented on May 27, 2024

Thank you for releasing this wonderful library!
As for the interface design idea, how about the following?
In my personal project, I am using a custom ShaderView interface as follows.

binding.shaderView.apply {
    initialize {
        fragmentShaderRawResId(R.raw.distort_texture)
        updateContinuously(true)
    }
    distortTextureShaderParameters {
        uPointer(Vec2f(followPointerX, followPointerY))
        uVelo(0f)
        resolution(Vec4f(0f, 0f, 0f, 0f))
        uTexture(Texture2D(textureResourceId = R.drawable.bokeh))
    }
    onDrawFrame {
        viewHeight = measuredHeight.toFloat()
        viewWidth = measuredWidth.toFloat()
        ...
        uPointer(Vec2f(followPointerX / 1000, followPointerY / 1000))
        resolution(
            Vec4f(viewWidth, viewHeight, resZ, resW)
        )
        uVelo(min(targetSpeed / 100, 0.5f))
    }
   ...
}

In the current ShaderView, when passing uniforms to shaders, the paramName and its value are passed separately.
The current interface is easy enough to use, but if the uniforms passed to shaders were modeled, it would be easier to use the same shader in multiple ShaderViews.

I will briefly explain how I implement the above interface.

1. Modeling unifroms

First, define unifroms as classes that can be passed to shaders. (example)

data class Vec3i(
    val x: Int,
    val y: Int,
    val z: Int,
) {
    fun toArray(): IntArray = intArrayOf(x, y, z)
}

Then, uniforms that are actually passed to the shader are grouped into a class annotated @ShaderParameters.(example)

@ShaderParameters is provided by the library.

@ShaderParameters
data class DistortTextureShaderParameters(
    val uPointer: Vec2f,
    val uTexture: Texture2D,
    val uVelo: Float,
    val resolution: Vec4f,
)

2. Build and Generate Extension Functions

After creating the uniform model and building the project, the extension functions are generated by KSP(or kapt)γ€€and KotlinPoet.

The ${ShaderParameters}Builder and ${ShaderParameters}Updater classes and the extension functions to use them are generated.

${ShaderParameters}Builder

The ${ShaderParameters}Builder holds the ShaderParamsBuilder, and methods are defined to add uniforms to it.
It would be convenient if the generated method is the same as the name of the uniform.

public class DistortTextureShaderParametersBuilder {
  private val shaderParamsBuilder: ShaderParamsBuilder = ShaderParamsBuilder()

  public fun uPointer(uniform: Vec2f?): Unit {
    shaderParamsBuilder.addVec2f("uPointer", uniform?.toArray())
  }

   ...
  public fun uVelo(uniform: Float?): Unit {
    shaderParamsBuilder.addFloat("uVelo", uniform)
  }

  public fun build(): ShaderParams = shaderParamsBuilder.build()
}

${ShaderParameters}Updater

The ${ShaderParameters}Updater holds the ShaderParams and provides methods to update the value of the uniform.

Again, it would be easier to understand if the name of the generated method is the same as uniform.

public class DistortTextureShaderParametersUpdater(
  private val shaderParams: ShaderParams
) {
  public fun uPointer(uniform: Vec2f): Unit {
    shaderParams.updateValue("uPointer", uniform.toArray())
  }

  public fun uTexture(uniform: Texture2D, needToRecycleWhenUploaded: Boolean = false): Unit {
    val textureResourceId = uniform.textureResourceId
    val bitmap = uniform.bitmap
    if (textureResourceId != null) {
        shaderParams.updateValue2D("uTexture", textureResourceId)
    } else if (bitmap != null) {
        shaderParams.updateValue2D("uTexture", bitmap, needToRecycleWhenUploaded)
    }
  }

  public fun uVelo(uniform: Float): Unit {
    shaderParams.updateValue("uVelo", uniform)
  }

  public fun resolution(uniform: Vec4f): Unit {
    shaderParams.updateValue("resolution", uniform.toArray())
  }

  ...
  public fun newBuilder(): ShaderParamsBuilder = shaderParams.newBuilder()
}

ShaderViewExtension.kt

After generating the above Builder and Updater, the following extension functions are generated.

public
    fun ShaderView.distortTextureShaderParameters(initializer: DistortTextureShaderParametersBuilder.() -> Unit):
    Unit {
  val parameters =
      com.appspell.shaderview.demo.distortion.DistortTextureShaderParametersBuilder().run {
      initializer()
      build()
  }
  shaderParams = parameters
}

public fun ShaderView.onDrawFrame(listener: DistortTextureShaderParametersUpdater.() -> Unit):
    Unit {
  onDrawFrameListener = { shaderParams ->
      val updater =
      com.appspell.shaderview.demo.distortion.DistortTextureShaderParametersUpdater(shaderParams)
      updater.listener()
  }
}

This method allows library users to use the first proposed interface.

How code generation is performed

Please refer to the sample project below for generating the code described here.
This sample project uses KSP and KotlinPoet.

branch: feature/kotlin_dsl_interface

https://github.com/tkhskt/ShaderView/tree/feature/kotlin_dsl_interface/processor

Sample Project

I created an activity called DistortionActivity.kt in the demo module of the following project. You can find an example of its use there.

branch: feature/kotlin_dsl_interface

https://github.com/tkhskt/ShaderView/tree/feature/kotlin_dsl_interface

If you like this idea, please message me and I will send you a PR anytime :)

Sorry for ranting...

from shaderview.

Related Issues (8)

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.