转载知乎小球布片碰撞模型在X86架构macOS上运行的问题

转载太极图形官方在知乎上的demo,代码如下,运行环境为pycharm,python==3.8.1,macOS==12.3.1,Vulkan手动安装1.3.216版本,Tiachi==1.0.2。机器配置为 :
CPU:i9-9980HK
图形卡:AMD Radeon Pro 5500M 8 GB;
Intel UHD Graphics 630 1536 MB

import taichi as ti
ti.init(arch=ti.cpu)  # 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)

@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., 0.5, 1)
        else:
            colors[i * n + j] = (1, 0.5, 0.)


initialize_mesh_indices()

spring_offsets = []
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(v):
        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((0, 0, 0))
scene = ti.ui.Scene()
camera = ti.ui.make_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.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.7, 0, 0))
    canvas.scene(scene)
    window.show()

运行结果:


可以发现球的碰撞体积比视觉体积大得多,在代码133行把0.95修改为其他数字没有解决问题

# Draw a smaller ball to avoid visual penetration
    scene.particles(ball_center, radius=ball_radius * 0.95, color=(0.7, 0, 0))

其次,如果我在把ti.init(arch=cpu)修改为ti.init(arch=gpu)或者ti.init(arch=metal)会出现如下报错:

[Taichi] version 1.0.3, llvm 10.0.0, commit fae94a21, osx, python 3.9.6
[Taichi] Starting on arch=x64
[mvk-info] MoltenVK version 1.1.9, supporting Vulkan version 1.1.211.
	The following 73 Vulkan extensions are supported:
		VK_KHR_16bit_storage v1
		VK_KHR_8bit_storage v1
		VK_KHR_bind_memory2 v1
		VK_KHR_create_renderpass2 v1
		VK_KHR_dedicated_allocation v3
		VK_KHR_depth_stencil_resolve v1
		VK_KHR_descriptor_update_template v1
		VK_KHR_device_group v4
		VK_KHR_device_group_creation v1
		VK_KHR_driver_properties v1
		VK_KHR_external_fence v1
		VK_KHR_external_fence_capabilities v1
		VK_KHR_external_memory v1
		VK_KHR_external_memory_capabilities v1
		VK_KHR_external_semaphore v1
		VK_KHR_external_semaphore_capabilities v1
		VK_KHR_get_memory_requirements2 v1
		VK_KHR_get_physical_device_properties2 v2
		VK_KHR_get_surface_capabilities2 v1
		VK_KHR_imageless_framebuffer v1
		VK_KHR_image_format_list v1
		VK_KHR_maintenance1 v2
		VK_KHR_maintenance2 v1
		VK_KHR_maintenance3 v1
		VK_KHR_multiview v1
		VK_KHR_portability_subset v1
		VK_KHR_push_descriptor v2
		VK_KHR_relaxed_block_layout v1
		VK_KHR_sampler_mirror_clamp_to_edge v3
		VK_KHR_sampler_ycbcr_conversion v14
		VK_KHR_shader_draw_parameters v1
		VK_KHR_shader_float16_int8 v1
		VK_KHR_shader_subgroup_extended_types v1
		VK_KHR_storage_buffer_storage_class v1
		VK_KHR_surface v25
		VK_KHR_swapchain v70
		VK_KHR_swapchain_mutable_format v1
		VK_KHR_timeline_semaphore v2
		VK_KHR_uniform_buffer_standard_layout v1
		VK_KHR_variable_pointers v1
		VK_EXT_debug_marker v4
		VK_EXT_debug_report v10
		VK_EXT_debug_utils v2
		VK_EXT_descriptor_indexing v2
		VK_EXT_fragment_shader_interlock v1
		VK_EXT_hdr_metadata v2
		VK_EXT_host_query_reset v1
		VK_EXT_image_robustness v1
		VK_EXT_inline_uniform_block v1
		VK_EXT_memory_budget v1
		VK_EXT_metal_surface v1
		VK_EXT_post_depth_coverage v1
		VK_EXT_private_data v1
		VK_EXT_robustness2 v1
		VK_EXT_sample_locations v1
		VK_EXT_scalar_block_layout v1
		VK_EXT_shader_stencil_export v1
		VK_EXT_shader_viewport_index_layer v1
		VK_EXT_subgroup_size_control v2
		VK_EXT_swapchain_colorspace v4
		VK_EXT_texel_buffer_alignment v1
		VK_EXT_texture_compression_astc_hdr v1
		VK_EXT_vertex_attribute_divisor v3
		VK_AMD_gpu_shader_half_float v2
		VK_AMD_negative_viewport_height v1
		VK_AMD_shader_image_load_store_lod v1
		VK_AMD_shader_trinary_minmax v1
		VK_IMG_format_pvrtc v1
		VK_INTEL_shader_integer_functions2 v1
		VK_GOOGLE_display_timing v1
		VK_MVK_macos_surface v3
		VK_MVK_moltenvk v34
		VK_NV_glsl_shader v1
[mvk-info] GPU device:
		model: AMD Radeon Pro 5500M
		type: Discrete
		vendorID: 0x1002
		deviceID: 0x7340
		pipelineCacheUUID: 0000277D-0C03-07D2-0000-000000000000
	supports the following Metal Versions, GPU's and Feature Sets:
		Metal Shading Language 2.3
		GPU Family Mac 2
		GPU Family Mac 1
		GPU Family Common 3
		GPU Family Common 2
		GPU Family Common 1
		macOS GPU Family 2 v1
		macOS GPU Family 1 v4
		macOS GPU Family 1 v3
		macOS GPU Family 1 v2
		macOS GPU Family 1 v1
		macOS Read-Write Texture Tier 2
[mvk-info] GPU device:
		model: Intel(R) UHD Graphics 630
		type: Integrated
		vendorID: 0x8086
		deviceID: 0x3e9b
		pipelineCacheUUID: 0000277D-0C03-07D2-0000-000000000000
	supports the following Metal Versions, GPU's and Feature Sets:
		Metal Shading Language 2.3
		GPU Family Mac 2
		GPU Family Mac 1
		GPU Family Common 3
		GPU Family Common 2
		GPU Family Common 1
		macOS GPU Family 2 v1
		macOS GPU Family 1 v4
		macOS GPU Family 1 v3
		macOS GPU Family 1 v2
		macOS GPU Family 1 v1
[mvk-info] Created VkInstance for Vulkan version 1.0.0, as requested by app, with the following 0 Vulkan extensions enabled:
[mvk-info] MoltenVK version 1.1.9, supporting Vulkan version 1.1.211.
	The following 73 Vulkan extensions are supported:
		VK_KHR_16bit_storage v1
		VK_KHR_8bit_storage v1
		VK_KHR_bind_memory2 v1
		VK_KHR_create_renderpass2 v1
		VK_KHR_dedicated_allocation v3
		VK_KHR_depth_stencil_resolve v1
		VK_KHR_descriptor_update_template v1
		VK_KHR_device_group v4
		VK_KHR_device_group_creation v1
		VK_KHR_driver_properties v1
		VK_KHR_external_fence v1
		VK_KHR_external_fence_capabilities v1
		VK_KHR_external_memory v1
		VK_KHR_external_memory_capabilities v1
		VK_KHR_external_semaphore v1
		VK_KHR_external_semaphore_capabilities v1
		VK_KHR_get_memory_requirements2 v1
		VK_KHR_get_physical_device_properties2 v2
		VK_KHR_get_surface_capabilities2 v1
		VK_KHR_imageless_framebuffer v1
		VK_KHR_image_format_list v1
		VK_KHR_maintenance1 v2
		VK_KHR_maintenance2 v1
		VK_KHR_maintenance3 v1
		VK_KHR_multiview v1
		VK_KHR_portability_subset v1
		VK_KHR_push_descriptor v2
		VK_KHR_relaxed_block_layout v1
		VK_KHR_sampler_mirror_clamp_to_edge v3
		VK_KHR_sampler_ycbcr_conversion v14
		VK_KHR_shader_draw_parameters v1
		VK_KHR_shader_float16_int8 v1
		VK_KHR_shader_subgroup_extended_types v1
		VK_KHR_storage_buffer_storage_class v1
		VK_KHR_surface v25
		VK_KHR_swapchain v70
		VK_KHR_swapchain_mutable_format v1
		VK_KHR_timeline_semaphore v2
		VK_KHR_uniform_buffer_standard_layout v1
		VK_KHR_variable_pointers v1
		VK_EXT_debug_marker v4
		VK_EXT_debug_report v10
		VK_EXT_debug_utils v2
		VK_EXT_descriptor_indexing v2
		VK_EXT_fragment_shader_interlock v1
		VK_EXT_hdr_metadata v2
		VK_EXT_host_query_reset v1
		VK_EXT_image_robustness v1
		VK_EXT_inline_uniform_block v1
		VK_EXT_memory_budget v1
		VK_EXT_metal_surface v1
		VK_EXT_post_depth_coverage v1
		VK_EXT_private_data v1
		VK_EXT_robustness2 v1
		VK_EXT_sample_locations v1
		VK_EXT_scalar_block_layout v1
		VK_EXT_shader_stencil_export v1
		VK_EXT_shader_viewport_index_layer v1
		VK_EXT_subgroup_size_control v2
		VK_EXT_swapchain_colorspace v4
		VK_EXT_texel_buffer_alignment v1
		VK_EXT_texture_compression_astc_hdr v1
		VK_EXT_vertex_attribute_divisor v3
		VK_AMD_gpu_shader_half_float v2
		VK_AMD_negative_viewport_height v1
		VK_AMD_shader_image_load_store_lod v1
		VK_AMD_shader_trinary_minmax v1
		VK_IMG_format_pvrtc v1
		VK_INTEL_shader_integer_functions2 v1
		VK_GOOGLE_display_timing v1
		VK_MVK_macos_surface v3
		VK_MVK_moltenvk v34
		VK_NV_glsl_shader v1
[mvk-info] GPU device:
		model: AMD Radeon Pro 5500M
		type: Discrete
		vendorID: 0x1002
		deviceID: 0x7340
		pipelineCacheUUID: 0000277D-0C03-07D2-0000-000000000000
	supports the following Metal Versions, GPU's and Feature Sets:
		Metal Shading Language 2.3
		GPU Family Mac 2
		GPU Family Mac 1
		GPU Family Common 3
		GPU Family Common 2
		GPU Family Common 1
		macOS GPU Family 2 v1
		macOS GPU Family 1 v4
		macOS GPU Family 1 v3
		macOS GPU Family 1 v2
		macOS GPU Family 1 v1
		macOS Read-Write Texture Tier 2
[mvk-info] GPU device:
		model: Intel(R) UHD Graphics 630
		type: Integrated
		vendorID: 0x8086
		deviceID: 0x3e9b
		pipelineCacheUUID: 0000277D-0C03-07D2-0000-000000000000
	supports the following Metal Versions, GPU's and Feature Sets:
		Metal Shading Language 2.3
		GPU Family Mac 2
		GPU Family Mac 1
		GPU Family Common 3
		GPU Family Common 2
		GPU Family Common 1
		macOS GPU Family 2 v1
		macOS GPU Family 1 v4
		macOS GPU Family 1 v3
		macOS GPU Family 1 v2
		macOS GPU Family 1 v1
[mvk-info] Created VkInstance for Vulkan version 1.1.211, as requested by app, with the following 6 Vulkan extensions enabled:
		VK_KHR_external_memory_capabilities v1
		VK_KHR_external_semaphore_capabilities v1
		VK_KHR_get_physical_device_properties2 v2
		VK_KHR_surface v25
		VK_EXT_debug_utils v2
		VK_EXT_metal_surface v1
[mvk-info] Using MTLEvent for Vulkan semaphores.
[mvk-info] Created VkDevice to run on GPU AMD Radeon Pro 5500M with the following 9 Vulkan extensions enabled:
		VK_KHR_bind_memory2 v1
		VK_KHR_dedicated_allocation v3
		VK_KHR_external_memory v1
		VK_KHR_external_semaphore v1
		VK_KHR_get_memory_requirements2 v1
		VK_KHR_portability_subset v1
		VK_KHR_shader_float16_int8 v1
		VK_KHR_swapchain v70
		VK_KHR_variable_pointers v1
[mvk-info] Created 2 swapchain images with initial size (2048, 2048).
[I 06/17/22 10:29:18.870 50568] [vulkan_device_creator.cpp:pick_physical_device@364] Found Vulkan Device 0 (AMD Radeon Pro 5500M)
[I 06/17/22 10:29:18.870 50568] [vulkan_device_creator.cpp:pick_physical_device@364] Found Vulkan Device 1 (Intel(R) UHD Graphics 630)
[I 06/17/22 10:29:18.870 50568] [vulkan_device_creator.cpp:create_logical_device@432] Vulkan Device "AMD Radeon Pro 5500M" supports Vulkan 0 version 1.1.211
[W 06/17/22 10:29:18.870 50568] [vulkan_device_creator.cpp:create_logical_device@469] Potential non-conformant Vulkan implementation, enabling VK_KHR_portability_subset
[W 06/17/22 10:29:18.870 50568] [vulkan_device_creator.cpp:create_logical_device@537] Taichi GPU GUI requires wide lines support
[W 06/17/22 10:29:20.223 50568] [vulkan_device.cpp:buffer@615] Overriding last binding
[W 06/17/22 10:29:20.229 50568] [vulkan_device.cpp:buffer@615] Overriding last binding
GLFW Error 65537: The GLFW library is not initialized

从报错中看,有两点让我疑惑:

  1. 返回值中的Vulkan和MoltenVK和我安装的版本似乎不同
  2. 这和我有两个GPU有关联吗?

Hi @DonaldJTrump , 欢迎来到Taichi论坛。

  1. 球的碰撞体积比视觉体积大得多,在代码133行把0.95修改为其他数字没有解决问题。
    这个问题的原因是目前GGUI中particles使用point primative 来画的,半径超过一定大小就不会clamp到最大的半径。正在尝试解决这个问题:GitHub - FantasyVR/taichi at ggui_particle

  2. 目前Taichi在Release的时候会把MoltenVK的lib一起Release,所以可能和你本机安装版本不同。你说的报错是ggui不能显示么?

  3. GLFW Error 65537: The GLFW library is not initialized 目前这个错误应该不影响GGUI的使用。