Code Monkey home page Code Monkey logo

taichi_glsl's Introduction

Taichi GLSL

Taichi GLSL is an extension library of the Taichi Programming Language, which provides a set of useful helper functions including but not limited to:

  1. Handy scalar functions like clamp, smoothstep, mix, round.
  2. GLSL-alike vector functions like normalize, distance, reflect.
  3. Well-behaved random generators including randUnit3D, randNDRange.
  4. Handy vector and matrix initializer: vec and mat.
  5. Handy vector component shuffle accessor like v.xy.
  6. Handy field sampler including bilerp and sample.
  7. Useful physics helper functions like boundReflect.
  8. Shadertoy-alike inputed GUI base class Animation.

[Clike me for documentation]

Build Status Documentation Status Coverage Status Downloads Latest Release

Installation

Install Taichi and Taichi GLSL with pip:

# Python 3.6/3.7/3.8 (64-bit)
pip install taichi taichi_glsl

How to play

First, import Taichi and Taichi GLSL:

import taichi as ti
import taichi_glsl as ts

Then, use ts.xxx helper functions in your Taichi kernels like this:

@ti.kernel
def kern():
  a = ts.vec(2.2, -3.3)   # deduced to be vec2
  b = ts.normalize(a)     # get normalized vector
  c = ts.clamp(a)         # element-wise, clamp to range [0, 1]
  d = int(a)              # cast to ivec2, vector of integers
  print(b, c, d)          # [0.554700, -0.832050] [1.000000, 0.000000] [2, -3]

Hints

If you don't like the ts. prefix, import using:

from taichi_glsl import *

@ti.kernel
def kern():
  a = vec(2.33, 6.66)
  b = normalize(a)
  ...

Note that this will import taichi as name ti as well.


vec2, vec3 and vec4 are simply vec in Taichi GLSL:

v = vec(2.0, 3.0)            # vec2
v = vec(2.0, 3.0, 4.0)       # vec3
v = vec(2.0, 3.0, 4.0, 5.0)  # vec4
v = vec(2, 3)                # ivec2 (since 2 is an integer)

Thanks to the python syntax of vec(*args).

Example

The following codes shows up an Shadertoy-style rainbow UV in the window:

import taichi as ti
import taichi_glsl as ts

ti.init()


class MyAnimation(ts.Animation):
    def on_init(self):
        self.img = ti.Vector(3, ti.f32, (512, 512))
        self.define_input()

    @ti.kernel
    def on_render(self):
        for I in ti.grouped(self.img):
            uv = I / self.iResolution
            self.img[I] = ti.cos(uv.xyx + self.iTime +
                                 ts.vec(0, 2, 4)) * 0.5 + 0.5


MyAnimation().start()

Check out more examples in the examples/ folder.

Links

taichi_glsl's People

Contributors

ailzhang avatar archibate avatar jack12xl avatar k-ye avatar ljcc0930 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

taichi_glsl's Issues

AttributeError: module 'taichi' has no attribute 'pyfunc'

[Taichi] version 0.8.12, llvm 10.0.0, commit 2cc32047, osx, python 3.8.2
[TaiGLSL] version 0.0.11
Traceback (most recent call last):
  File "demo-test.py", line 2, in <module>
    from taichi_glsl import *
  File "/Users/puffy/Library/Python/3.8/lib/python/site-packages/taichi_glsl/__init__.py", line 10, in <module>
    from .hack import *
  File "/Users/puffy/Library/Python/3.8/lib/python/site-packages/taichi_glsl/hack.py", line 37, in <module>
    @ti.pyfunc
  File "/Users/puffy/Library/Python/3.8/lib/python/site-packages/taichi/__init__.py", line 56, in __getattr__
    raise AttributeError(f"module '{__name__}' has no attribute '{attr}'")
AttributeError: module 'taichi' has no attribute 'pyfunc'

About ti.pyfunc

About ti.pyfunc

So ti.pyfunc can be called from both Taichi and py scope.

So is there any reason for the existence of ti.func(except for it takes time to refactor the previous code ?) for now?

If ti.func is kind of obsolete, could you update the ti.func in this repo to ti.pyfunc? Because that make it easier to debug😂, since we do not need to explicitly call @ti.kernel in our code to test.

Error occurred when running example sdf_rt1.py

[E 08/18/20 00:35:32.302] [codegen_llvm.cpp:visit@301] Assertion failure: from != to

                        * Taichi Core - Stack Traceback *                             

==========================================================================================
| Module | Offset | Function |
|----------------------------------------------------------------------------------------|

  •           taichi_core.so |     110 | taichi::Logger::error(std::__1::basic_string< |
                                       | char, std::__1::char_traits<char>, std::__1:: |
                                       | allocator<char> > const&, bool)               |
    
  •           taichi_core.so |    3378 | taichi::lang::CodeGenLLVM::visit(taichi::lang |
                                       | ::UnaryOpStmt*)                               |
    
  •           taichi_core.so |      43 | taichi::lang::CodeGenLLVM::visit(taichi::lang |
                                       | ::Block*)                                     |
    
  •           taichi_core.so |     786 | taichi::lang::CodeGenLLVMCPU::create_offload_ |
                                       | range_for(taichi::lang::OffloadedStmt*)       |
    
  •           taichi_core.so |    1159 | taichi::lang::CodeGenLLVMCPU::visit(taichi::l |
                                       | ang::OffloadedStmt*)                          |
    
  •           taichi_core.so |      43 | taichi::lang::CodeGenLLVM::visit(taichi::lang |
                                       | ::Block*)                                     |
    
  •           taichi_core.so |     103 | taichi::lang::CodeGenLLVM::emit_to_module()   |
    
  •           taichi_core.so |      25 | taichi::lang::CodeGenLLVM::gen()              |
    
  •           taichi_core.so |     123 | taichi::lang::CodeGenCPU::codegen()           |
    
  •           taichi_core.so |      97 | taichi::lang::KernelCodeGen::compile()        |
    
  •           taichi_core.so |     251 | taichi::lang::Program::compile(taichi::lang:: |
                                       | Kernel&)                                      |
    
  •           taichi_core.so |      62 | taichi::lang::Kernel::compile()               |
    
  •           taichi_core.so |     136 | taichi::lang::Kernel::operator()()            |
    
  •           taichi_core.so |     103 | void pybind11::cpp_function::initialize<taich |
                                       | i::export_lang(pybind11::module&)::$_9, void, |
                                       |  taichi::lang::Kernel*, pybind11::name, pybin |
                                       | d11::is_method, pybind11::sibling>(taichi::ex |
                                       | port_lang(pybind11::module&)::$_9&&, void (*) |
                                       | (taichi::lang::Kernel*), pybind11::name const |
                                       | &, pybind11::is_method const&, pybind11::sibl |
                                       | ing const&)::'lambda'(pybind11::detail::funct |
                                       | ion_call&)::__invoke(pybind11::detail::functi |
                                       | on_call&)                                     |
    
  •           taichi_core.so |    4075 | pybind11::cpp_function::dispatcher(_object*,  |
                                       | _object*, _object*)                           |
    
  •                   python |     483 | (null)                                        |
    
  •                   python |     111 | (null)                                        |
    
  •                   python |     130 | (null)                                        |
    
  •                   python |     130 | (null)                                        |
    
  •                   python |     370 | (null)                                        |
    
  •                   python |     179 | (null)                                        |
    
  •                   python |     453 | (null)                                        |
    
  •                   python |   46111 | (null)                                        |
    
  •                   python |     414 | (null)                                        |
    
  •                   python |     231 | (null)                                        |
    
  •                   python |   46668 | (null)                                        |
    
  •                   python |     414 | (null)                                        |
    
  •                   python |     231 | (null)                                        |
    
  •                   python |     189 | (null)                                        |
    
  •                   python |     130 | (null)                                        |
    
  •                   python |   46668 | (null)                                        |
    
  •                   python |     414 | (null)                                        |
    
  •                   python |     195 | (null)                                        |
    
  •                   python |     183 | (null)                                        |
    
  •                   python |   46111 | (null)                                        |
    
  •                   python |     414 | (null)                                        |
    
  •                   python |     256 | (null)                                        |
    
  •                   python |     391 | (null)                                        |
    
  •                   python |    9635 | (null)                                        |
    
  •                   python |     125 | (null)                                        |
    
  •            libdyld.dylib |       1 | (null)                                        |
    
  •                      ??? |       2 | (null)                                        |
    

==========================================================================================

Internal Error occurred, check this page for possible solutions:
https://taichi.readthedocs.io/en/stable/install.html#troubleshooting
Traceback (most recent call last):
File "/Users/limber/Documents/code/taichi_glsl/examples/sdf_rt1.py", line 85, in
render()
File "/Users/limber/anaconda3/lib/python3.7/site-packages/taichi/lang/kernel.py", line 578, in wrapped
return primal(*args, **kwargs)
File "/Users/limber/anaconda3/lib/python3.7/site-packages/taichi/lang/kernel.py", line 506, in call
return self.compiled_functionskey
File "/Users/limber/anaconda3/lib/python3.7/site-packages/taichi/lang/kernel.py", line 466, in func__
t_kernel()
RuntimeError: [codegen_llvm.cpp:visit@301] Assertion failure: from != to

Process finished with exit code 1

Can only iterate through Taichi fields/snodes

import taichi as ti
import taichi_glsl as ts

ti.init(arch=ti.gpu)

@ti.func
def unroll(v):
    for i in ti.static(range(3)):
        v[i]= 0

@ti.data_oriented
class Test:
    def __init__(self):
        self.v  = ti.Vector.field(3, dtype=ti.f32)
        ti.root.dense(ti.i, 100 ).place(self.v) 

    @ti.kernel
    def test(self):
        for  i in self.v:
            unroll(self.v[i])

test1 = Test()
test1.test()
  • The code will catch an error "Can only iterate through Taichi fields/snodes"
  • But I find if I comment "import taichi_glsl as ts", it works well

About ts.TaichiClass

Hi, I'm the guy who posted this.

I'm currently considering to refactor my project to make it easier to extend. So I remembered in that post you mentioned the ts.TaichiClass, which is an enhanced version for ti.data_oriented.

Could you explain more about what's the difference between these two class because I did not find a concrete example(except the complex class from glsl) or documentation?

  1. For example, for the question I raised in that post, how exactly the ts.TaichiClass could make a difference?

  2. Could I directly replace every @ti.data_oriented decorator in a typical taichi class to ts.TaichiClass ?

    • I mean a class decorated with @ti.data_oriented and a class inherenteded from ts.TaichiClass. Are they the same (except the extra function of ts.TaichiClass)?

Sorry my python skill could not support me to master the exact difference from source code

Thanks in advance !

Updating to latest Taichi

Great library, thanks for sharing!

I would be very interested to use it but unfortunately it no longer works with latest Taichi due to several breaking changes

Is there any interest in bringing the library up to date? I don't know enough Taichi yet myself to help so much.

TaichiOperations class access in Complex class

Recently I see this on freshly installed instances:

[Taichi] mode=release
[Taichi] preparing sandbox at /tmp/taichi-utbwh7jc
[Taichi] version 0.7.16, llvm 10.0.0, commit bca037cf, linux, python 3.8.5
[TaiGLSL] version 0.0.10
Traceback (most recent call last):
  File "branch.py", line 61, in <module>
    import taichi_glsl as ts
  File "/usr/local/lib/python3.8/dist-packages/taichi_glsl/__init__.py", line 24, in <module>
    from .classes import *
  File "/usr/local/lib/python3.8/dist-packages/taichi_glsl/classes.py", line 5, in <module>
    class Complex(tl.TaichiClass, ti.TaichiOperations):
AttributeError: module 'taichi' has no attribute 'TaichiOperations'

Changing ti.TaichiOperations to ti.lang.common_ops.TaichiOperations in the Complex definition of taichi_glsl/classes.py seems to fix things.

Need to release a new version

The latest version (0.0.8) doesn't work because this line is not included in the release.

import taichi_glsl as tl

This affects examples (e.g. mass_spring_3d). Following error will be raised. Maybe we should release a new version? 🤔

[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/59/8w1k63jj0vs_rv19jwyf1nf00000gn/T/taichi-f1cw8gi3
[Taichi] version 0.6.29, llvm 10.0.0, commit f1bde29b, osx, python 3.8.5
[TaiGLSL] version 0.0.8
[TaiGLSL] Inputs are welcomed at https://github.com/taichi-dev/taichi_glsl
[Tai3D] version 0.0.3
[Taichi] Starting on arch=x64
[Taichi] materializing...
Traceback (most recent call last):
  File "references/mass_spring_2d.py", line 105, in <module>
    substep()
  File "/Users/[REDACTED]/venv/lib/python3.8/site-packages/taichi/lang/kernel.py", line 580, in wrapped
    return primal(*args, **kwargs)
  File "/Users/[REDACTED]/venv/lib/python3.8/site-packages/taichi/lang/kernel.py", line 507, in __call__
    self.materialize(key=key, args=args, arg_features=arg_features)
  File "/Users/[REDACTED]/venv/lib/python3.8/site-packages/taichi/lang/kernel.py", line 378, in materialize
    taichi_kernel = taichi_kernel.define(taichi_ast_generator)
  File "/Users/[REDACTED]/venv/lib/python3.8/site-packages/taichi/lang/kernel.py", line 375, in taichi_ast_generator
    compiled()
  File "references/mass_spring_2d.py", line 52, in substep
    v[i] = tl.ballBoundReflect(x[i], v[i], tl.vec(+0.0, +0.2, -0.0), 0.4,
  File "/Users/[REDACTED]/venv/lib/python3.8/site-packages/taichi/lang/kernel.py", line 42, in decorated
    return fun.__call__(*args)
  File "/Users/[REDACTED]/venv/lib/python3.8/site-packages/taichi/lang/kernel.py", line 87, in __call__
    ret = self.compiled(*args)
  File "/Users/[REDACTED]/venv/lib/python3.8/site-packages/taichi_glsl/lagrangian.py", line 43, in ballBoundReflect
    above = tl.distance(pos, center) - radius
NameError: name 'tl' is not defined

AttributeError: type object 'Matrix' has no attribute '_element_wise_binary'

In hack.py, the statement _old_element_wise_binary = ti.Matrix._element_wise_binary generates directly an AttributeError.

steps to reproduce (python3.11) :

  1. pip install taichi taichi_glsl

in a file example.py:
2) import taichi_glsl as ts

this statement will do from .hack import * that generates the error:

AttributeError: type object 'Matrix' has no attribute '_element_wise_binary'

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.