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

Texture load in @ti.func #7464

Closed
afiretony opened this issue Mar 1, 2023 · 7 comments
Closed

Texture load in @ti.func #7464

afiretony opened this issue Mar 1, 2023 · 7 comments
Assignees
Labels
question Question on using Taichi

Comments

@afiretony
Copy link

Hi,
I have a question related with texture load in Taichi.
In example python/taichi/examples/rendering/simple_texture.py, the texture object is declared out of taichi scope, and passed as ti.types.texture type to @ti.kernel function. It seemed that the texture will be converted to taichi.lang._texture.TextureSampler object type.
My question is that how can use texture method like sample_lod or fetch in @ti.func?

@afiretony afiretony added the question Question on using Taichi label Mar 1, 2023
@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Mar 1, 2023
@bobcao3
Copy link
Collaborator

bobcao3 commented Mar 1, 2023

You need to pass the texture in @ti.template (pass-by-reference)

@afiretony
Copy link
Author

Hi @bobcao3 , thanks for answering!
Could you elaborate what do you meant to pass in ti.template?
This is what I tried to do but not works:

texture = ti.Texture(ti.Format.r32f, (n, n, n))

@ti.kernel
def paint(t: ti.f32, n: ti.i32):
    for i, j in pixels:
        uv = ti.Vector([i / res[0], j / res[1], t])
        c = ti.math.vec4(0.0)
        c = sample_tex(texture, uv)
        pixels[i, j] = [c.r, c.r, c.r]

@ti.func
def sample_tex(tex:ti.template(), uv):
    tex.sample_lod(uv, 0.0)

AttributeError: 'Texture' object has no attribute 'sample_lod'

@bobcao3
Copy link
Collaborator

bobcao3 commented Mar 1, 2023

You would also need to pass the texture into the kernel. Taichi textures can not be captured through global varaibles

@afiretony
Copy link
Author

afiretony commented Mar 1, 2023

Hey @bobcao3 thanks, do you mean

@ti.kernel
def paint(t: ti.f32, tex: ti.types.texture(num_dimensions=3), n: ti.i32):
    for i, j in pixels:
        uv = ti.Vector([i / res[0], j / res[1], t])
        c = ti.math.vec4(0.0)
        c = sample_tex(tex, uv)
        pixels[i, j] = [c.r, c.r, c.r]

@ti.func
def sample_tex(tex:ti.template(), uv):
    tex.sample_lod(uv, 0.0)

But still gets error:

Traceback (most recent call last):
File "playground.py", line 93, in
main()
File "playground.py", line 86, in main
paint(t,texture, n)
File "C:\Users\Anaconda3\envs\taichi\lib\site-packages\taichi\lang\kernel_impl.py", line 974, in wrapped
return primal(*args, **kwargs)
File "C:\Users\Anaconda3\envs\taichi\lib\site-packages\taichi\lang\kernel_impl.py", line 901, in call
return self.runtime.compiled_functionskey
File "C:\Users\Anaconda3\envs\taichi\lib\site-packages\taichi\lang\kernel_impl.py", line 826, in func__
raise e from None
File "C:\Users\Anaconda3\envs\taichi\lib\site-packages\taichi\lang\kernel_impl.py", line 823, in func__
t_kernel(launch_ctx)
RuntimeError: [type_factory.cpp:taichi::lang::promoted_type@222] Assertion failure: a->is() && b->is()

@bobcao3
Copy link
Collaborator

bobcao3 commented Mar 1, 2023

Hmmmmm. That seems like a bug? Let me check

@erizmr erizmr moved this from Untriaged to Todo in Taichi Lang Mar 3, 2023
@ailzhang
Copy link
Contributor

ailzhang commented Mar 9, 2023

@afiretony sorry for the late reply, this is not a bug since your ti.func is missing a return but we didn't do well in error reporting lol.

This works for me:

@ti.func
def sample_tex(tex: ti.template(), uv):
    return tex.sample_lod(uv, 0.0)

@ti.kernel
def paint(t: ti.f32, pixels: ti.types.ndarray(ndim=2),
          tex: ti.types.texture(num_dimensions=2)):
    for i, j in pixels:
        uv = ti.Vector([i / res[0], j / res[1]])
        warp_uv = uv + ti.Vector(
            [ti.cos(t + uv.x * 5.0),
             ti.sin(t + uv.y * 5.0)]) * 0.1
        c = ti.math.vec4(0.0)
        if uv.x > 0.5:
            # c = tex.sample_lod(warp_uv, 0.0)
            c = sample_tex(tex, warp_uv)
        else:
            c = tex.fetch(ti.cast(warp_uv * 128, ti.i32), 0)
        pixels[i, j] = [c.r, c.r, c.r, 1.0]

Let us know if you have followup questions! We'll try to improve the error message here.

@afiretony
Copy link
Author

Thank you @ailzhang for pointing that out! Didn't notice such an obvious mistake here hahaha.

@github-project-automation github-project-automation bot moved this from Todo to Done in Taichi Lang Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question on using Taichi
Projects
Status: Done
Development

No branches or pull requests

3 participants