taichi-dev / taichi

Productive, portable, and performant GPU programming in Python.
https://taichi-lang.org
Apache License 2.0
25.56k stars 2.29k forks source link

[GGUI] Meshes disappear when using ti.arch=ti.vulkan #8254

Open willi-z opened 1 year ago

willi-z commented 1 year ago

Describe the bug I am new to taichi tried the example with cpu -> worked fine! Switched vulkan on-> did not see the mesh! A quick search resulted in my bug being possibly related to #5571. It was apperently fixed, so why did I have the same issue now? Can somebody help me?

To Reproduce

import taichi as ti
ti.init(arch=ti.vulkan)  # Alternatively, ti.init(arch=ti.cpu)

n = 128
quad_size = 1.0 / n
dt = 4e-2 / n
substeps = int(1 / 60 // dt)

gravity = ti.Vector([0, -9.8, 0])
spring_Y = 3e4
dashpot_damping = 1e4
drag_damping = 1

ball_radius = 0.3
ball_center = ti.Vector.field(3, dtype=float, shape=(1, ))
ball_center[0] = [0, 0, 0]

x = ti.Vector.field(3, dtype=float, shape=(n, n))
v = ti.Vector.field(3, dtype=float, shape=(n, n))

num_triangles = (n - 1) * (n - 1) * 2
indices = ti.field(int, shape=num_triangles * 3)
vertices = ti.Vector.field(3, dtype=float, shape=n * n)
colors = ti.Vector.field(3, dtype=float, shape=n * n)

bending_springs = False

@ti.kernel
def initialize_mass_points():
    random_offset = ti.Vector([ti.random() - 0.5, ti.random() - 0.5]) * 0.1

    for i, j in x:
        x[i, j] = [
            i * quad_size - 0.5 + random_offset[0], 0.6,
            j * quad_size - 0.5 + random_offset[1]
        ]
        v[i, j] = [0, 0, 0]

@ti.kernel
def initialize_mesh_indices():
    for i, j in ti.ndrange(n - 1, n - 1):
        quad_id = (i * (n - 1)) + j
        # 1st triangle of the square
        indices[quad_id * 6 + 0] = i * n + j
        indices[quad_id * 6 + 1] = (i + 1) * n + j
        indices[quad_id * 6 + 2] = i * n + (j + 1)
        # 2nd triangle of the square
        indices[quad_id * 6 + 3] = (i + 1) * n + j + 1
        indices[quad_id * 6 + 4] = i * n + (j + 1)
        indices[quad_id * 6 + 5] = (i + 1) * n + j

    for i, j in ti.ndrange(n, n):
        if (i // 4 + j // 4) % 2 == 0:
            colors[i * n + j] = (0.22, 0.72, 0.52)
        else:
            colors[i * n + j] = (1, 0.334, 0.52)

initialize_mesh_indices()

spring_offsets = []
if bending_springs:
    for i in range(-1, 2):
        for j in range(-1, 2):
            if (i, j) != (0, 0):
                spring_offsets.append(ti.Vector([i, j]))

else:
    for i in range(-2, 3):
        for j in range(-2, 3):
            if (i, j) != (0, 0) and abs(i) + abs(j) <= 2:
                spring_offsets.append(ti.Vector([i, j]))

@ti.kernel
def substep():
    for i in ti.grouped(x):
        v[i] += gravity * dt

    for i in ti.grouped(x):
        force = ti.Vector([0.0, 0.0, 0.0])
        for spring_offset in ti.static(spring_offsets):
            j = i + spring_offset
            if 0 <= j[0] < n and 0 <= j[1] < n:
                x_ij = x[i] - x[j]
                v_ij = v[i] - v[j]
                d = x_ij.normalized()
                current_dist = x_ij.norm()
                original_dist = quad_size * float(i - j).norm()
                # Spring force
                force += -spring_Y * d * (current_dist / original_dist - 1)
                # Dashpot damping
                force += -v_ij.dot(d) * d * dashpot_damping * quad_size

        v[i] += force * dt

    for i in ti.grouped(x):
        v[i] *= ti.exp(-drag_damping * dt)
        offset_to_center = x[i] - ball_center[0]
        if offset_to_center.norm() <= ball_radius:
            # Velocity projection
            normal = offset_to_center.normalized()
            v[i] -= min(v[i].dot(normal), 0) * normal
        x[i] += dt * v[i]

@ti.kernel
def update_vertices():
    for i, j in ti.ndrange(n, n):
        vertices[i * n + j] = x[i, j]

window = ti.ui.Window("Taichi Cloth Simulation on GGUI", (1024, 1024),
                      vsync=True)
canvas = window.get_canvas()
canvas.set_background_color((1, 1, 1))
scene = ti.ui.Scene()
camera = ti.ui.Camera()

current_t = 0.0
initialize_mass_points()

while window.running:
    if current_t > 1.5:
        # Reset
        initialize_mass_points()
        current_t = 0

    for i in range(substeps):
        substep()
        current_t += dt
    update_vertices()

    camera.position(0.0, 0.0, 3)
    camera.lookat(0.0, 0.0, 0)
    scene.set_camera(camera)

    scene.point_light(pos=(0, 1, 2), color=(1, 1, 1))
    scene.ambient_light((0.5, 0.5, 0.5))
    scene.mesh(vertices,
               indices=indices,
               per_vertex_color=colors,
               two_sided=True)

    # Draw a smaller ball to avoid visual penetration
    scene.particles(ball_center, radius=ball_radius * 0.95, color=(0.5, 0.42, 0.8))
    canvas.scene(scene)
    window.show()

Log/Screenshots Please post the full log of the program (instead of just a few lines around the error message, unless the log is > 1000 lines). This will help us diagnose what's happening. For example:

$ python python clov_sim.py 
[Taichi] version 1.6.0, llvm 15.0.4, commit f1c6fbbd, linux, python 3.11.3
[Taichi] Starting on arch=vulkan

further the newest version of the vulkan SDK (1.3.250.1) is installed and successfully tested.

Diagnose output

$ ti diagnose
[Taichi] version 1.6.0, llvm 15.0.4, commit f1c6fbbd, linux, python 3.11.3

*******************************************
**      Taichi Programming Language      **
*******************************************

Docs:   https://docs.taichi-lang.org/
GitHub: https://github.com/taichi-dev/taichi/
Forum:  https://forum.taichi.graphics/

Taichi system diagnose:

python: 3.11.3 (main, May 24 2023, 00:00:00) [GCC 13.1.1 20230511 (Red Hat 13.1.1-2)]
system: linux
executable: <censored>/taichi_tests/.venv/bin/python
platform: Linux-6.3.8-200.fc38.x86_64-x86_64-with-glibc2.37
architecture: 64bit ELF
uname: uname_result(system='Linux', node='s0up', release='6.3.8-200.fc38.x86_64', version='#1 SMP PREEMPT_DYNAMIC Thu Jun 15 02:15:40 UTC 2023', machine='x86_64')
<censored>/taichi_tests/.venv/lib64/python3.11/site-packages/taichi/tools/diagnose.py:20: DeprecationWarning: Use setlocale(), getencoding() and getlocale() instead
  print(f'locale: {".".join(locale.getdefaultlocale())}')
locale: en_GB.UTF-8
PATH: <censored>

LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: Fedora
Description:    Fedora release 38 (Thirty Eight)
Release:        38
Codename:       ThirtyEight

import: <module 'taichi' from '<censored>/taichi_tests/.venv/lib64/python3.11/site-packages/taichi/__init__.py'>

cc: False
cpu: True
metal: False
opengl: True
[W 07/03/23 17:14:09.676 96148] [cuda_driver.cpp:load_lib@36] libcuda.so lib not found.
cuda: False
vulkan: True

`glewinfo` not available: [Errno 2] No such file or directory: 'glewinfo'

`nvidia-smi` not available: [Errno 2] No such file or directory: 'nvidia-smi'
[Taichi] version 1.6.0, llvm 15.0.4, commit f1c6fbbd, linux, python 3.11.3

[Taichi] version 1.6.0, llvm 15.0.4, commit f1c6fbbd, linux, python 3.11.3
[Taichi] Starting on arch=x64

[Taichi] version 1.6.0, llvm 15.0.4, commit f1c6fbbd, linux, python 3.11.3
[Taichi] Starting on arch=opengl

[W 07/03/23 17:14:13.247 96223] [cuda_driver.cpp:load_lib@36] libcuda.so lib not found.
[W 07/03/23 17:14:13.248 96223] [misc.py:adaptive_arch_select@753] Arch=[<Arch.cuda: 4>] is not supported, falling back to CPU
[Taichi] version 1.6.0, llvm 15.0.4, commit f1c6fbbd, linux, python 3.11.3
[Taichi] Starting on arch=x64

[Taichi] version 1.6.0, llvm 15.0.4, commit f1c6fbbd, linux, python 3.11.3

*******************************************
**      Taichi Programming Language      **
*******************************************

Docs:   https://docs.taichi-lang.org/
GitHub: https://github.com/taichi-dev/taichi/
Forum:  https://forum.taichi.graphics/

                                   TAICHI EXAMPLES                                    
 ──────────────────────────────────────────────────────────────────────────────────── 
  0: ad_gravity               25: keyboard                50: pbf2d                   
  1: circle_packing_image     26: laplace                 51: physarum                
  2: comet                    27: laplace_equation        52: poisson_disk_sampling   
  3: cornell_box              28: mandelbrot_zoom         53: print_offset            
  4: diff_sph                 29: marching_squares        54: rasterizer              
  5: euler                    30: mass_spring_3d_ggui     55: regression              
  6: eulerfluid2d             31: mass_spring_game        56: sdf_renderer            
  7: explicit_activation      32: mass_spring_game_ggui   57: simple_derivative       
  8: export_mesh              33: mciso_advanced          58: simple_texture          
  9: export_ply               34: mgpcg                   59: simple_uv               
  10: export_videos           35: mgpcg_advanced          60: snow_phaseField         
  11: fem128                  36: minimal                 61: stable_fluid            
  12: fem128_ggui             37: minimization            62: stable_fluid_ggui       
  13: fem99                   38: mpm128                  63: stable_fluid_graph      
  14: fractal                 39: mpm128_ggui             64: taichi_bitmasked        
  15: fractal3d_ggui          40: mpm3d                   65: taichi_dynamic          
  16: fullscreen              41: mpm3d_ggui              66: taichi_logo             
  17: game_of_life            42: mpm88                   67: taichi_ngp              
  18: gui_image_io            43: mpm88_graph             68: taichi_sparse           
  19: gui_widgets             44: mpm99                   69: texture_graph           
  20: implicit_fem            45: mpm_lagrangian_forces   70: tutorial                
  21: implicit_mass_spring    46: nbody                   71: two_stream_instability  
  22: initial_value_problem   47: odop_solar              72: vortex_rings            
  23: jacobian                48: oit_renderer            73: waterwave               
  24: karman_vortex_street    49: patterns                                            
 ──────────────────────────────────────────────────────────────────────────────────── 
42
Running example minimal ...
[Taichi] Starting on arch=x64
42.0
>>> Running time: 0.44s

Consider attaching this log when maintainers ask about system information.
>>> Running time: 8.91s
bobcao3 commented 1 year ago

Hi, what's your GPU? This might be a GPU dependent bug

willi-z commented 1 year ago

AMD Radeon Vega 10 Graphics (raven, LLVM 16.0.5, DRM 3.52, 6.3.8-200.fc38.x86_64)