Skip to content

A Compose Multiplatform library for GPU shader effects

Notifications You must be signed in to change notification settings

Debanshu777/ShaderX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

20 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

ShaderX ๐ŸŽจ

A Kotlin Multiplatform library for GPU shader effects in Jetpack Compose, supporting Android, iOS, and Desktop (Windows, macOS, Linux). Apply stunning visual effects with a simple, type-safe API.

Kotlin Compose Multiplatform License API

๐ŸŽฏ What is ShaderX?

ShaderX is a cross-platform shader effects library that brings GPU-powered visual effects to Compose Multiplatform applications. It provides an intuitive Modifier-based API for applying effects like grayscale, blur, pixelation, and wave animations to any composable. Built using modern Kotlin Multiplatform development practices, it offers native performance across Android, iOS, and Desktop platforms.

๐Ÿ“ฑ Demo

Android iOS Desktop
Coming Soon Coming Soon Coming Soon

โœจ Key Features

  • Cross-Platform Support

    • ๐Ÿค– Android (API 33+ with AGSL)
    • ๐ŸŽ iOS (arm64, Simulator)
    • ๐Ÿ–ฅ๏ธ Desktop (Windows, macOS, Linux with Skia)
  • Rich Effects Library

    • ๐ŸŒซ๏ธ Blur - Hardware-accelerated Gaussian blur
    • ๐ŸŽจ Grayscale - Convert to grayscale using luminance weights
    • ๐Ÿ“ธ Sepia - Apply vintage sepia tone
    • ๐Ÿ”ฒ Pixelate - Create retro pixelation effects
    • ๐ŸŒ€ Vignette - Darken image edges
    • ๐ŸŒˆ Chromatic Aberration - Simulate lens color separation
    • ๐Ÿ”„ Invert - Invert all colors
    • ๐ŸŒŠ Wave - Animated wave distortion
    • ๐ŸŽญ Gradient - Custom gradient overlays
  • Developer Experience

    • ๐Ÿ”’ Type-safe API with strongly typed parameters
    • โšก Compose Modifier extensions for easy integration
    • ๐ŸŽฌ Built-in animation support
    • ๐Ÿ› ๏ธ Custom shader support (AGSL/SkSL)
    • โŒ Comprehensive error handling with ShaderResult<T>
    • ๐Ÿ”— Effect chaining with CompositeEffect

๐Ÿš€ Getting Started

Installation

Add the dependency to your build.gradle.kts:

// settings.gradle.kts
dependencyResolutionManagement {
    repositories {
        mavenCentral()
        maven { url = uri("https://jitpack.io") }
    }
}

// build.gradle.kts
dependencies {
    implementation("com.github.Debanshu777.ShaderX:shaderx:0.1.1")
}

Quick Start

Apply a simple effect:

import com.debanshu.shaderlab.shaderx.compose.shaderEffect
import com.debanshu.shaderlab.shaderx.effects.GrayscaleEffect

@Composable
fun GrayscaleImage() {
    Image(
        painter = painterResource("photo.png"),
        contentDescription = null,
        modifier = Modifier.shaderEffect(GrayscaleEffect())
    )
}

Use animated effects:

import com.debanshu.shaderlab.shaderx.compose.rememberShaderEffect
import com.debanshu.shaderlab.shaderx.effects.WaveEffect

@Composable
fun AnimatedWaveImage() {
    val waveEffect = rememberShaderEffect(WaveEffect(amplitude = 10f))
    
    Image(
        painter = painterResource("photo.png"),
        contentDescription = null,
        modifier = Modifier.shaderEffect(waveEffect)
    )
}

Adjust parameters dynamically:

val effect = GrayscaleEffect(intensity = 0.5f)
val updatedEffect = effect.withParameter("intensity", 0.8f)

๐ŸŽจ Built-in Effects

Effect Description Animated
GrayscaleEffect Converts to grayscale using luminance weights โŒ
SepiaEffect Applies vintage sepia tone โŒ
VignetteEffect Darkens image edges with customizable radius โŒ
NativeBlurEffect Hardware-accelerated Gaussian blur โŒ
PixelateEffect Creates retro pixelation with adjustable block size โŒ
ChromaticAberrationEffect Simulates lens color separation โŒ
InvertEffect Inverts all color channels โŒ
WaveEffect Animated wave distortion effect โœ…
GradientEffect Custom gradient overlay โŒ

๐Ÿ› ๏ธ Creating Custom Effects

data class MyEffect(
    private val intensity: Float = 1f
) : RuntimeShaderEffect {

    override val id = "my_effect"
    override val displayName = "My Effect"

    override val shaderSource = """
        uniform shader content;
        uniform float intensity;
        
        half4 main(float2 fragCoord) {
            half4 color = content.eval(fragCoord);
            return half4(color.rgb * intensity, color.a);
        }
    """

    override val parameters = listOf(
        PercentageParameter("intensity", "Intensity", intensity)
    )

    override fun buildUniforms(width: Float, height: Float) = listOf(
        FloatUniform("intensity", intensity)
    )

    override fun withParameter(id: String, value: Float) = when (id) {
        "intensity" -> copy(intensity = value)
        else -> this
    }
}

โŒ Error Handling

The library uses ShaderResult<T> for operations that may fail:

val factory = ShaderFactory.create()
val result = factory.createRenderEffect(effect, width, height)

result
    .onSuccess { renderEffect ->
        // Apply the effect
    }
    .onFailure { error ->
        when (error) {
            is ShaderError.CompilationError -> log("Shader compile failed: ${error.message}")
            is ShaderError.UnsupportedEffect -> log("Effect not supported: ${error.effectId}")
            is ShaderError.PlatformNotSupported -> log("Platform limitation: ${error.message}")
            else -> log("Unknown error: ${error.message}")
        }
    }

๐Ÿ—๏ธ Architecture & Technical Stack

Technology Stack

Category Technology Version
Language Kotlin 2.3.0
UI Framework Compose Multiplatform 1.10.0
Build System Gradle (AGP) 9.0.0
Android SDK Compile/Target SDK 36
Coroutines kotlinx-coroutines 1.10.2
Serialization kotlinx-serialization 1.9.0

Platform-Specific Backends

Platform Shader Backend Min Version
Android AGSL (RuntimeShader) API 33
iOS Skia iOS 14+
Desktop Skia JDK 11+

Project Structure

ShaderLab/
โ”œโ”€โ”€ ๐Ÿ“ shaderx/                         # Core library module
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ ๐Ÿ“ commonMain/              # Shared Kotlin code
โ”‚       โ”‚   โ””โ”€โ”€ kotlin/com/debanshu/shaderlab/shaderx/
โ”‚       โ”‚       โ”œโ”€โ”€ ShaderX.kt          # Main entry point
โ”‚       โ”‚       โ”œโ”€โ”€ ShaderConstants.kt  # Shared constants
โ”‚       โ”‚       โ”œโ”€โ”€ compose/            # Compose Modifier integration
โ”‚       โ”‚       โ”œโ”€โ”€ effect/             # Core effect interfaces
โ”‚       โ”‚       โ”‚   โ”œโ”€โ”€ ShaderEffect.kt
โ”‚       โ”‚       โ”‚   โ”œโ”€โ”€ RuntimeShaderEffect.kt
โ”‚       โ”‚       โ”‚   โ”œโ”€โ”€ AnimatedShaderEffect.kt
โ”‚       โ”‚       โ”‚   โ”œโ”€โ”€ NativeEffect.kt
โ”‚       โ”‚       โ”‚   โ”œโ”€โ”€ CompositeEffect.kt
โ”‚       โ”‚       โ”‚   โ””โ”€โ”€ impl/           # Built-in effect implementations
โ”‚       โ”‚       โ”œโ”€โ”€ effects/            # Public effect APIs
โ”‚       โ”‚       โ”œโ”€โ”€ factory/            # Platform-specific shader factories
โ”‚       โ”‚       โ”œโ”€โ”€ parameter/          # Parameter types & formatting
โ”‚       โ”‚       โ”œโ”€โ”€ uniform/            # Shader uniform types
โ”‚       โ”‚       โ””โ”€โ”€ result/             # Error handling (ShaderResult)
โ”‚       โ”œโ”€โ”€ ๐Ÿ“ androidMain/             # Android-specific (AGSL)
โ”‚       โ”œโ”€โ”€ ๐Ÿ“ iosMain/                 # iOS-specific (Skia)
โ”‚       โ”œโ”€โ”€ ๐Ÿ“ jvmMain/                 # Desktop-specific (Skia)
โ”‚       โ””โ”€โ”€ ๐Ÿ“ skiaMain/                # Shared Skia implementation
โ”œโ”€โ”€ ๐Ÿ“ samples/
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ ShaderLab/                   # Demo application
โ”‚   โ”‚   โ”œโ”€โ”€ composeApp/                 # Shared compose code
โ”‚   โ”‚   โ”œโ”€โ”€ androidApp/                 # Android launcher
โ”‚   โ”‚   โ”œโ”€โ”€ iosApp/                     # iOS Xcode project
โ”‚   โ”‚   โ””โ”€โ”€ imagelib/                   # Image loading utilities
โ”‚   โ””โ”€โ”€ ๐Ÿ“ VerticalCarousel/            # Additional sample
โ””โ”€โ”€ ๐Ÿ“ gradle/
    โ””โ”€โ”€ libs.versions.toml              # Version catalog

Core Abstractions

Interface Purpose
ShaderEffect Base interface for all shader effects
RuntimeShaderEffect Effects using custom AGSL/SkSL shaders
AnimatedShaderEffect Effects with time-based animations
NativeEffect Platform-native effects (e.g., hardware blur)
CompositeEffect Chain multiple effects together

๐Ÿ“ฆ Build Commands

Library

# Build library for all platforms
./gradlew :shaderx:build

# Publish to local Maven
./gradlew :shaderx:publishToMavenLocal

Sample App - Android

# Build debug APK
./gradlew :samples:ShaderLab:androidApp:assembleDebug

# Install on connected device
./gradlew :samples:ShaderLab:androidApp:installDebug

Sample App - iOS (macOS only)

# Build framework for simulator
./gradlew :samples:ShaderLab:composeApp:linkDebugFrameworkIosSimulatorArm64

Then open samples/ShaderLab/iosApp/iosApp.xcodeproj in Xcode and run.

Sample App - Desktop

# Run directly
./gradlew :samples:ShaderLab:composeApp:run

# Package for distribution
./gradlew :samples:ShaderLab:composeApp:packageDmg    # macOS
./gradlew :samples:ShaderLab:composeApp:packageMsi    # Windows
./gradlew :samples:ShaderLab:composeApp:packageDeb    # Linux

๐Ÿ”ฎ Roadmap

  • Additional effects (Glitch, Noise, Film Grain)
  • Effect presets and themes
  • Shader parameter animation curves
  • Compose preview support
  • Effect export/import (JSON)
  • Shader hot-reload for development
  • Web (WASM) support
  • Video/camera effect support

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Guidelines

  • Follow Kotlin coding conventions
  • Use Compose best practices
  • Maintain clean architecture principles
  • Add appropriate test coverage

๐Ÿ“„ License

Copyright 2024 Debanshu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

๐Ÿ™ Acknowledgments


ShaderX - GPU-powered visual effects across platforms ๐ŸŽจโœจ

About

A Compose Multiplatform library for GPU shader effects

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages