Skip to main content

Self-Contained OpenGL Rendering Pipelines for Python

Project description

ZenGL

ZenGL

pip install zengl

Concept

ZenGL is a low level graphics library.

  • ZenGL turns OpenGL into Vulkan like Rendering Pipelines
  • ZenGL uses OpenGL 3.3 Core and OpenGL 3.0 ES - runs everywhere
  • ZenGL provides a developer friendly experience in protyping complex scenes

ZenGL emerged from an experimental version of ModernGL. To keep ModernGL backward compatible, ZenGL was re-designed from the ground-up to support a strict subset of OpenGL. On the other hand, ModernGL supports a wide variety of OpenGL versions and extensions.

ZenGL is "ModernGL hard mode"

ZenGL intentionally does not support:

  • Transform Feedback
  • Geometry Shaders
  • Tesselation
  • Compute Shaders
  • 3D Textures
  • Storage Buffers

Most of the above can be implemented in a more hardware friendly way using the existing ZenGL API. Interoperability with other modules is also possible. Using such may reduce the application's portablity. It is even possible to use direct OpenGL calls together with ZenGL, however this is likely not necessary.

It is common to render directly to the screen with OpenGL. With ZenGL, the right way is to render to a framebuffer and blit the final image to the screen. This allows fine-grained control of the framebuffer format, guaranteed multisampling settings, correct depth/stencil precison. It is also possible to render directly to the screen, however this feature is designed to be used for the postprocessing step.

This design allows ZenGL to support:

  • Rendering without a window
  • Rendering to multiple windows
  • Rendering to HDR monitors
  • Refreshing the screen without re-rendering the scene
  • Apply post-processing without changing how the scene is rendered
  • Making reusable shaders and components
  • Taking screenshots or exporting a video

The default framebuffer in OpenGL is highly dependent on how the Window is created. It is often necessary to configure the Window to provide the proper depth precision, stencil buffer, multisampling and double buffering. Often the "best pixel format" lacks all of these features on purpose. ZenGL aims to allow choosing these pixel formats and ensures the user specifies the rendering requirements. It is even possible to render low-resolution images and upscale them for high-resolution monitors. Tearing can be easily prevented by decoupling the scene rendering from the screen updates.

ZenGL was designed for Prototyping

It is tempting to start a project with Vulkan, however even getting a simple scene rendered requires tremendous work and advanced tooling to compile shaders ahead of time. ZenGL provides self-contained Pipelines which can be easily ported to Vulkan. ZenGL code is verbose and easy to read.

ZenGL support multiple design patters

Many libraries enfore certain design patterns. ZenGL avoids this by providing cached pipeline creation, pipeline templating and lean resourece and framebuffer definition. It is supported to create pipelines on the fly or template them for certain use-cases.

TODO: examples for such patters

Disambiguation

  • ZenGL is a drop-in replacement for pure OpenGL code
  • Using ZenGL requires some OpenGL knowledge
  • ZenGL Images are OpenGL Texture Objects or OpenGL Renderbuffer Objects
  • ZenGL Buffers are OpenGL Buffer Objects
  • ZenGL Pipelines contain an OpenGL Vertex Array Object, an OpenGL Program Object, and an OpenGL Framebuffer Object
  • ZenGL Pielines may also contain OpenGL Sampler Objects
  • Creating ZenGL Pipelines does not necessarily compile the shader from source
  • The ZenGL Shader Cache exists independently from the Pipeline objects
  • A Framebuffer is always represented by a Python list of ZenGL Images
  • There is no Pipeline.clear() method, individual images must be cleared independently
  • GLSL Uniform Blocks and sampler2D objects are bound in the Pipeline layout
  • Textures and Uniform Buffers are bound in the Pipeline resources

Examples

bezier_curves deferred_rendering envmap fractal grass normal_mapping rigged_objects wireframe

Simple Pipeline Definition

pipeline = ctx.pipeline(
    # program definition
    vertex_shader='...',
    fragment_shader='...',
    layout=[
        {
            'name': 'Uniforms',
            'binding': 0,
        },
        {
            'name': 'Texture',
            'binding': 0,
        },
    ],

    # descriptor sets
    resources=[
        {
            'type': 'uniform_buffer',
            'binding': 0,
            'buffer': uniform_buffer,
        },
        {
            'type': 'sampler',
            'binding': 0,
            'image': texture,
        },
    ],

    # uniforms
    uniforms={
        'color': [0.0, 0.5, 1.0],
        'iterations': 10,
    },

    # program definition global state
    depth={
        'func': 'less',
        'write': False,
    },
    stencil={
        'front': {
            'fail_op': 'replace',
            'pass_op': 'replace',
            'depth_fail_op': 'replace',
            'compare_op': 'always',
            'compare_mask': 1,
            'write_mask': 1,
            'reference': 1,
        },
        'back': ...,
        # or
        'both': ...,
    },
    blend={
        'enable': True,
        'src_color': 'src_alpha',
        'dst_color': 'one_minus_src_alpha',
    },
    cull_face='back',
    topology='triangles',

    # framebuffer
    framebuffer=[color1, color2, ..., depth],
    viewport=(x, y, width, height),

    # vertex array
    vertex_buffers=[
        *zengl.bind(vertex_buffer, '3f 3f', 0, 1), # bound vertex attributes
        *zengl.bind(None, '2f', 2), # unused vertex attribute
    ],
    index_buffer=index_buffer, # or None
    short_index=False, # 2 or 4 byte intex
    vertex_count=...,
    instance_count=1,
    first_vertex=0,

    # override includes
    includes={
        'common': '...',
    },
)

# some members are actually mutable and calls no OpenGL functions
pipeline.viewport = ...
pipeline.vertex_count = ...
pipeline.uniforms['iterations'][:] = struct.pack('i', 50) # writable memoryview

# rendering
pipeline.render() # no parameters for hot code

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zengl-2.4.1.tar.gz (54.7 kB view hashes)

Uploaded Source

Built Distributions

zengl-2.4.1-cp311-abi3-win_amd64.whl (47.4 kB view hashes)

Uploaded CPython 3.11+ Windows x86-64

zengl-2.4.1-cp311-abi3-win32.whl (40.4 kB view hashes)

Uploaded CPython 3.11+ Windows x86

zengl-2.4.1-cp311-abi3-musllinux_1_1_x86_64.whl (134.1 kB view hashes)

Uploaded CPython 3.11+ musllinux: musl 1.1+ x86-64

zengl-2.4.1-cp311-abi3-musllinux_1_1_i686.whl (123.8 kB view hashes)

Uploaded CPython 3.11+ musllinux: musl 1.1+ i686

zengl-2.4.1-cp311-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (133.7 kB view hashes)

Uploaded CPython 3.11+ manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

zengl-2.4.1-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (123.1 kB view hashes)

Uploaded CPython 3.11+ manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

zengl-2.4.1-cp311-abi3-macosx_11_0_arm64.whl (44.6 kB view hashes)

Uploaded CPython 3.11+ macOS 11.0+ ARM64

zengl-2.4.1-cp311-abi3-macosx_10_9_x86_64.whl (47.1 kB view hashes)

Uploaded CPython 3.11+ macOS 10.9+ x86-64

zengl-2.4.1-cp310-cp310-win_amd64.whl (47.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

zengl-2.4.1-cp310-cp310-win32.whl (40.3 kB view hashes)

Uploaded CPython 3.10 Windows x86

zengl-2.4.1-cp310-cp310-musllinux_1_1_x86_64.whl (135.3 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

zengl-2.4.1-cp310-cp310-musllinux_1_1_i686.whl (125.8 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

zengl-2.4.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (131.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

zengl-2.4.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (122.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

zengl-2.4.1-cp310-cp310-macosx_11_0_arm64.whl (44.4 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

zengl-2.4.1-cp310-cp310-macosx_10_9_x86_64.whl (47.0 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

zengl-2.4.1-cp39-cp39-win_amd64.whl (47.2 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

zengl-2.4.1-cp39-cp39-win32.whl (40.3 kB view hashes)

Uploaded CPython 3.9 Windows x86

zengl-2.4.1-cp39-cp39-musllinux_1_1_x86_64.whl (134.8 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

zengl-2.4.1-cp39-cp39-musllinux_1_1_i686.whl (125.3 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

zengl-2.4.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (131.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

zengl-2.4.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (121.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

zengl-2.4.1-cp39-cp39-macosx_11_0_arm64.whl (44.4 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

zengl-2.4.1-cp39-cp39-macosx_10_9_x86_64.whl (47.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

zengl-2.4.1-cp38-cp38-win_amd64.whl (47.2 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

zengl-2.4.1-cp38-cp38-win32.whl (40.3 kB view hashes)

Uploaded CPython 3.8 Windows x86

zengl-2.4.1-cp38-cp38-musllinux_1_1_x86_64.whl (139.6 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

zengl-2.4.1-cp38-cp38-musllinux_1_1_i686.whl (129.6 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

zengl-2.4.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (134.5 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

zengl-2.4.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (125.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

zengl-2.4.1-cp38-cp38-macosx_11_0_arm64.whl (44.4 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

zengl-2.4.1-cp38-cp38-macosx_10_9_x86_64.whl (47.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page