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

Cannot resize GraphicsBuffer using p3tinydisplay (segfault) #1322

Closed
duburcqa opened this issue Jun 12, 2022 · 1 comment
Closed

Cannot resize GraphicsBuffer using p3tinydisplay (segfault) #1322

duburcqa opened this issue Jun 12, 2022 · 1 comment
Assignees
Labels
Milestone

Comments

@duburcqa
Copy link

duburcqa commented Jun 12, 2022

Description

It throws an error message when calling graphics_engine.render_frame() and crashes with a segfault right after resizing a GraphicsBuffer that has been initialized with the height or width smaller than the requested one.

Assertion failed: xmin >= 0 && xmin < _c->zb->xsize && ymin >= 0 && ymin < _c->zb->ysize && xmin + xsize >= 0 && xmin + xsize <= _c->zb->xsize && ymin + ysize >= 0 && ymin + ysize <= _c->zb->ysize at line 286 of panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx

Steps to Reproduce

Comment or uncomment the line win.setSize(1024, 1024).

import numpy as np
import matplotlib.pyplot as plt

from direct.showbase.ShowBase import ShowBase
from panda3d.core import (
    FrameBufferProperties, WindowProperties, PerspectiveLens,
    GraphicsPipe, AntialiasAttrib, loadPrcFileData,  GeomVertexFormat,
    GeomVertexData, GeomVertexWriter, GeomLines, Geom, GeomNode)


class Viewer(ShowBase):
    def __init__(self):
        # Enforce viewer configuration
        loadPrcFileData("", """
            win-size 512 512
            win-fixed-size 0
            window-type onscreen
            load-display p3tinydisplay
        """)

        # Initialize showbase
        super().__init__()

        # Initialize offscreen buffers
        self.buffers = []
        self.offcameras = []

    def add_offscreen_camera(self):
        # Set offscreen buffer frame properties
        fbprops = FrameBufferProperties(self.win.getFbProperties())
        fbprops.set_accum_bits(0)
        fbprops.set_back_buffers(0)

        # Set offscreen buffer windows properties
        winprops = WindowProperties()
        winprops.set_size(*self.win.get_size())

        # Set offscreen buffer flags to enforce resizeable `GraphicsBuffer`
        flags = GraphicsPipe.BF_refuse_window | GraphicsPipe.BF_refuse_parasite
        flags |= GraphicsPipe.BF_resizeable

        # Create new offscreen buffer
        win_name = f"offscreen_buffer[{len(self.buffers)}]"
        win = self.graphicsEngine.make_output(
            self.pipe, win_name, 0, fbprops, winprops, flags,
            self.win.get_gsg(), self.win)
        win.setSize(1024, 1024)
        self.buffers.append(win)

        # Append buffer to the list of windows managed by the ShowBase
        self.winList.append(win)

        # Create 3D camera region for the scene
        lens = PerspectiveLens()
        camera_name = f"offscreen_camera[{len(self.buffers)}]"
        camera = self.make_camera(win, camName=camera_name, lens=lens)
        self.offcameras.append(camera)

    def get_images(self):
        images = []
        for buffer in self.buffers:
            # Capture frame as raw texture
            texture = buffer.get_screenshot()

            # Extract raw array buffer from texture
            data = texture.get_ram_image_as('RGBA')

            # Convert raw texture to numpy array
            array = np.frombuffer(data, np.uint8)
            image = np.flipud(array.reshape((
                texture.get_y_size(), texture.get_x_size(), 4)))
            images.append(image)
        return images


if __name__ == "__main__":
    # Create the viewer
    sim = Viewer()

    # Place a box in the middle of the scene
    vformat = GeomVertexFormat.get_v3c4()
    vdata = GeomVertexData('vdata', vformat, Geom.UHStatic)
    vdata.uncleanSetNumRows(6)
    vertex = GeomVertexWriter(vdata, 'vertex')
    color = GeomVertexWriter(vdata, 'color')
    for x, y, z in np.eye(3):
        vertex.addData3(0, 0, 0)
        color.addData4(x, y, z, 1)
        vertex.addData3(x, y, z)
        color.addData4(x, y, z, 1)
    prim = GeomLines(Geom.UHStatic)
    prim.addNextVertices(6)
    geom = Geom(vdata)
    geom.addPrimitive(prim)
    model = GeomNode('axes')
    model.add_geom(geom)
    node = sim.render.attach_new_node(model)
    node.set_light_off()
    node.set_render_mode_wireframe()
    node.set_render_mode_thickness(4)
    node.set_antialias(AntialiasAttrib.MLine)
    node.set_scale(0.3)
    node.set_shader_off()
    node.setScale(1.0)
    node.setPos(-0.5, -0.5, 0)

    # Create offscreen camera
    n_cameras, camera_radius, camera_height = 3, 6, 4
    for i in range(n_cameras):
        sim.add_offscreen_camera()
        camera_angle = i * (2 * np.pi / n_cameras)
        pos = ( camera_radius * np.sin(camera_angle),
               -camera_radius * np.cos(camera_angle),
                camera_height)
        sim.offcameras[i].set_pos(pos)
        sim.offcameras[i].look_at(0, 0, 0)

    # Set main window camera pos
    sim.cam.set_pos(5, 0, 0)
    sim.cam.look_at(0, 0, 0)

    # Display offscreen camera images
    sim.task_mgr.step() #sim.graphics_engine.render_frame()
    images = sim.get_images()
    if images:
        fig, axes = plt.subplots(1, len(images))
        for ax, image in zip(axes, images):
            ax.imshow(image)
            ax.set_aspect('equal')
        plt.show(block=False)
        plt.pause(0.1)

    # Update the main window in background
    sim.run()

Environment

  • Operating system: Ubuntu 20.04
  • System architecture:
  • ThinkPad E14 Gen 2
  • 32GiB SODIMM DDR4 Synchronous 3200 MHz
  • 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  • Mesa Intel® Xe Graphics (TGL GT2)
  • SAMSUNG MZALQ512HALU-000L1 512GB NVMe
  • Panda3D version: 1.10.11
  • Installation method: pip
  • Python version (if using Python): 3.8.10
  • Compiler (if using C++): None
@duburcqa duburcqa changed the title Cannot resize GraphicsBuffer using p3tinydisplay Cannot resize GraphicsBuffer using p3tinydisplay (segfault) Jun 12, 2022
@rdb rdb added this to the 1.10.12 milestone Jun 29, 2022
@rdb rdb added the bug label Jun 29, 2022
@rdb rdb self-assigned this Jun 29, 2022
@rdb rdb closed this as completed in 3fc579c Jun 29, 2022
@duburcqa
Copy link
Author

Great news 👍 Much wow, many thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants