wolfgpu.gl_utils

Author: HECE - University of Liege, Stéphane Champailler, Pierre Archambeau Date: 2024

Copyright (c) 2024 University of Liege. All rights reserved.

This script and its content are protected by copyright law. Unauthorized copying or distribution of this file, via any medium, is strictly prohibited.

Module Contents

wolfgpu.gl_utils.GL_COLOR_ATTACHMENTS[source]
wolfgpu.gl_utils.TEXTURE_UNITS[source]
wolfgpu.gl_utils.TEX_SAMPLERS_RE[source]
wolfgpu.gl_utils.IMAGE_UNIT_RE[source]
wolfgpu.gl_utils.check_gl_error()[source]
wolfgpu.gl_utils.samplers_in_shader[source]
wolfgpu.gl_utils.samplers_in_shader: dict[tuple[int, str], str][source]
wolfgpu.gl_utils.shaders_programs: dict[int, int][source]
wolfgpu.gl_utils.shaders_names: dict[int, str][source]
wolfgpu.gl_utils.all_textures_sizes[source]
wolfgpu.gl_utils.textures_formats[source]
wolfgpu.gl_utils._set_uniform_cache[source]
wolfgpu.gl_utils.gl_clear_all_caches()[source]
wolfgpu.gl_utils.clear_uniform_cache()[source]
wolfgpu.gl_utils.describe_program(pid)[source]
wolfgpu.gl_utils.load_shader_from_source(shader_type, source: str) int[source]
wolfgpu.gl_utils.track_texture_size(tex_id, img_data, w, h, format) int[source]
wolfgpu.gl_utils.total_textures_size() int[source]
wolfgpu.gl_utils.rgb_to_rgba(t)[source]
wolfgpu.gl_utils.drop_textures(texture_ids: list[int] | int)[source]

Drop one or more textures. Expect texture id’s.

wolfgpu.gl_utils._get_texture_format_info(format, img_data)[source]
wolfgpu.gl_utils.update_texture(texture_id: int, xoffset: int, yoffset: int, img_data: numpy.ndarray)[source]
wolfgpu.gl_utils.upload_np_array_to_gpu(context, format, img_data: numpy.ndarray, texture_id: int | None = None) int[source]

The goal of this function is to standardize textures configuration and upload to GPU. We trade generality for ease of use.

If you pass in a texture_id, then the texture will be updated instead of created.

Returns:

the texture OpenGL id.

wolfgpu.gl_utils.memory_aligned_byte_array(size, value)[source]
wolfgpu.gl_utils.upload_blank_texture_to_gpu(w: int, h: int, context, format=GL_R32F, value=0.0, texture_id=None)[source]

Make and upload a blank (or one color) texture to the GPU.

format: some texture format. The way it is done here means that we will derive the texture format in the GPU as well as the texture format of the “value” data. context: either GL_TEXTURE_RECTANGLE, GL_TEXTURE_2D value: will be set on each components of the texels. texture_id: if you pass a texture ID, then the texture will be

updated (instead of creating a new one)

wolfgpu.gl_utils.clear_texture(texture_id)[source]
wolfgpu.gl_utils._get_format_type_from_internal_format(internal_format)[source]
wolfgpu.gl_utils.read_texture2(tex_id, desired_format, width: int = None, height: int = None) numpy.ndarray[source]

Read a texture tex_id out of the GPU and returns it as an array.

The desired_fromat is the one you want to get the texture in. Be aware that some formats are not supported by python OpenGL (right now, we know of RG16UI).

wolfgpu.gl_utils.read_texture(frame_buffer_id, color_attachment_ndx, width, height, internal_format)[source]

DEPRECATED Use the version 2 (this one needs framebuffers which is painful to manage).

Read a rectangle (0,0,width, height) in a texture of size at least (width, height).

FIXME Check things up with : https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGetFramebufferAttachmentParameter.xhtml

FIXME This code is limited as we read from a frame buffer (and not directly from a texture id). So one has to provide framebuffer and attachment number (which is not quite convenient).

The internal format of a texture can be found in the dictioneary textures_formats FIXME again, this is sub optimal. It’d be nicer to resolve a texture ID to its corresponding frame buffer/attachment (but it dosen’t make too much sense since a texture may be attached to several FB and since I sometimes hack the fb/textures directly (so I don’t maintaint an FB/attach <-> tex. id correspondance)

Parameters:
  • internal_format – !!! The return format !!! This wan not intended, but crept in the code.

  • color_attachment_ndx – either an int or a GL_COLOR_ATTACHMENTx

wolfgpu.gl_utils.load_shader_from_file(fpath: pathlib.Path, log_path: pathlib.Path = None)[source]
wolfgpu.gl_utils.create_frame_buffer_for_computation(destination_texture, out_to_fragdata=False, depth_buffer=False, texture_target=GL_TEXTURE_RECTANGLE)[source]

destination_texture: a texture or a list of textures. If list of textures, one frame buffer will be attached to each texture, via COLOR_ATTACHMENT0,1,2,… (in list order)

out_to_fragdata: if the FB will be used as output of a shader that issues FragData instead of colors. In that case, we create link buffers so that will receive these FragData.

depth_buffer: attach a depth buffer to the framebuffer. None or pass in the dimensions of the buffer. # FIXME don’t pass the dimensions, guess them from the texture.

wolfgpu.gl_utils.load_program(vertex_shader=None, fragment_shader=None, geometry_shader=None, compute_shader=None)[source]
wolfgpu.gl_utils.set_uniform(program, name, value)[source]
wolfgpu.gl_utils.set_texture(program, unif_name, tex_index, tex_unit)[source]
wolfgpu.gl_utils.wire_program(program: int, uniforms=dict(), textures=dict(), ssbos=dict())[source]

Binds texture to (read) sampler. Sets uniforms. This doesn’t touch the framebuffer bindings.

program : OpenGL id of the program uniforms: map uniform names (str) to their values textures: map texture sampler names to their texture id. Instead of texture_id you can pass a tuple (texture_id, access) where access is either: GL_READ_ONLY, GL_WRITE_ONLY, or GL_READ_WRITE. See https://www.khronos.org/opengl/wiki/Image_Load_Store

wolfgpu.gl_utils.upload_geometry_to_vao(triangles: numpy.ndarray, attr_loc: int = 0, normalized: bool = True)[source]

Geometry is a n rows * [x,y,z] columns matrix representing the vertices, each having x,y,z coordinates (NDC coordinates, that is, each in [-1,+1].

The vertices will be wired to the vertex attribute attr_loc

VAO only stores info about buffers (not transformation, not parameters, etc.)

Returns a Vertex Array Object.

wolfgpu.gl_utils.make_quad(xmin, xmax, ymin, ymax)[source]
wolfgpu.gl_utils.make_unit_quad(texture_width, texture_height)[source]
wolfgpu.gl_utils.query_gl_caps()[source]
wolfgpu.gl_utils.init_gl(width, height)[source]
wolfgpu.gl_utils.estimate_best_window_size(screen_width, screen_height, sim_dim_x, sim_dim_y)[source]

Given the screen size as (screen_width, screen_height) and the simulation domain size given as (sim_dim_x, sim_dim_y) returns a somewhat ideal window size as a tuple (width, height).

wolfgpu.gl_utils.init_pygame(domain_width, domain_height, iconify: bool = True) Callable[source]
wolfgpu.gl_utils.init_glfw(domain_width, domain_height) Callable[source]