File: scope.rst

package info (click to toggle)
python-moderngl 5.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,700 kB
  • sloc: python: 15,758; cpp: 14,665; makefile: 14
file content (121 lines) | stat: -rw-r--r-- 3,021 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Scope
=====

.. py:class:: Scope

    Returned by :py:meth:`Context.scope`

    This class represents a Scope object.

    Responsibilities on enter:

    - Set the enable flags.
    - Bind the framebuffer.
    - Assigning textures to texture locations.
    - Assigning buffers to uniform buffers.
    - Assigning buffers to shader storage buffers.

    Responsibilities on exit:

    - Restore the enable flags.
    - Restore the framebuffer.

Methods
-------

.. py:method:: Scope.release

Attributes
----------

.. py:attribute:: Scope.ctx
    :type: Context

    The context this object belongs to

.. py:attribute:: Scope.extra
    :type: Any

    User defined data.

Examples
--------

.. rubric:: Simple scope example

.. code-block:: python

    scope1 = ctx.scope(fbo1, moderngl.BLEND)
    scope2 = ctx.scope(fbo2, moderngl.DEPTH_TEST | moderngl.CULL_FACE)

    with scope1:
        # do some rendering

    with scope2:
        # do some rendering

.. rubric:: Scope for querying

.. code-block:: python

    query = ctx.query(samples=True)
    scope = ctx.scope(ctx.screen, moderngl.DEPTH_TEST | moderngl.RASTERIZER_DISCARD)

    with scope, query:
        # do some rendering

    print(query.samples)

.. rubric:: Understanding what scope objects do

.. code-block:: python

    scope = ctx.scope(
        framebuffer=framebuffer1,
        enable_only=moderngl.BLEND,
        textures=[
            (texture1, 4),
            (texture2, 3),
        ],
        uniform_buffers=[
            (buffer1, 6),
            (buffer2, 5),
        ],
        storage_buffers=[
            (buffer3, 8),
        ],
    )

    # Let's assume we have some state before entering the scope
    some_random_framebuffer.use()
    some_random_texture.use(3)
    some_random_buffer.bind_to_uniform_block(5)
    some_random_buffer.bind_to_storage_buffer(8)
    ctx.enable_only(moderngl.DEPTH_TEST)

    with scope:
        # on __enter__
        #     framebuffer1.use()
        #     ctx.enable_only(moderngl.BLEND)
        #     texture1.use(4)
        #     texture2.use(3)
        #     buffer1.bind_to_uniform_block(6)
        #     buffer2.bind_to_uniform_block(5)
        #     buffer3.bind_to_storage_buffer(8)

        # do some rendering

        # on __exit__
        #     some_random_framebuffer.use()
        #     ctx.enable_only(moderngl.DEPTH_TEST)

    # Originally we had the following, let's see what was changed
    some_random_framebuffer.use()                 # This was restored hurray!
    some_random_texture.use(3)                    # Have to restore it manually.
    some_random_buffer.bind_to_uniform_block(5)   # Have to restore it manually.
    some_random_buffer.bind_to_storage_buffer(8)  # Have to restore it manually.
    ctx.enable_only(moderngl.DEPTH_TEST)          # This was restored too.

    # Scope objects only do as much as necessary.
    # Restoring the framebuffer and enable flags are lowcost operations and
    # without them you could get a hard time debugging the application.