Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I need your ideas how to simplify interface! #18

Open
appspell opened this issue Jan 1, 2022 · 1 comment
Open

I need your ideas how to simplify interface! #18

appspell opened this issue Jan 1, 2022 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@appspell
Copy link
Owner

appspell commented Jan 1, 2022

I want to make the simpler way how to use this library.
Do you have any ideas on how to make the architecture or API library more friendly?

Look at the code below, seems like there is a better (simpler) way to set up this view. Maybe some DSL?

shaderView.apply {
                updateContinuously = true // update each frame
                vertexShaderRawResId = R.raw.quad_tangent_space_vert
                fragmentShaderRawResId = R.raw.nomral_map
                shaderParams = ShaderParamsBuilder()
                    .addTexture2D(
                        "uNormalTexture",
                        R.drawable.normal_button,
                        GLES30.GL_TEXTURE0
                    )
                    .addColor("uColor", R.color.grey, resources)
                    .addVec3f("uVaryingColor", floatArrayOf(0.5f, 0.5f, 0.5f))
                    .addVec3f("uLightDirection", floatArrayOf(1.0f, 1.0f, 0.0f))
                    .addVec3f("uEyeDirection", floatArrayOf(0.0f, 0.0f, 0.0f))
                    .build()
                onDrawFrameListener = { shaderParams ->
                    val pos = (System.currentTimeMillis() % 5000L) / 1000f
                    shaderParams.updateValue("uLightDirection", floatArrayOf(0.0f + pos, 1.0f, 0.0f))
                }
            }
@appspell appspell added the question Further information is requested label Jan 1, 2022
@appspell appspell self-assigned this Jan 1, 2022
@tkhskt
Copy link

tkhskt commented Mar 13, 2022

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants