syphon.utils.raw
1import ctypes 2from typing import Any, Optional 3 4import Metal 5 6 7def create_mtl_texture(device: Any, width: int, height: int, pixel_format: int = Metal.MTLPixelFormatRGBA8Unorm) -> Any: 8 """ 9 Create a Metal texture with the specified parameters. 10 11 Parameters: 12 - device (Any): The Metal device. 13 - width (int): The width of the texture. 14 - height (int): The height of the texture. 15 - pixel_format (int): The pixel format of the texture (default: MTLPixelFormatRGBA8Unorm). 16 17 Returns: 18 - Any: The created Metal texture. 19 """ 20 texture_descriptor = Metal.MTLTextureDescriptor.texture2DDescriptorWithPixelFormat_width_height_mipmapped_( 21 pixel_format, width, height, False 22 ) 23 24 return device.newTextureWithDescriptor_(texture_descriptor) 25 26 27def copy_bytes_to_mtl_texture(data: bytes, texture: Any): 28 """ 29 Copy pixel data from a bytes object to a Metal texture. 30 31 Parameters: 32 - data (bytes): The pixel data as bytes. 33 - texture (Any): The target Metal texture to copy the pixel data into. 34 """ 35 region = Metal.MTLRegion((0, 0, 0), (texture.width(), texture.height(), 1)) 36 bytes_per_row = texture.width() * 4 37 38 texture.replaceRegion_mipmapLevel_withBytes_bytesPerRow_( 39 region, 40 0, # mipmapLevel 41 data, 42 bytes_per_row, 43 ) 44 45 46def copy_mtl_texture_to_bytes(texture: Any, buffer: Optional[Any] = None) -> bytes: 47 """ 48 Copy pixel data from a Metal texture to a bytes object. 49 50 Parameters: 51 - texture (Any): The source Metal texture to copy pixel data from. 52 - buffer (Optional[Any]): The buffer to store the result. If None, a new buffer will be created. 53 54 Returns: 55 - bytes: The resulting pixel data as bytes. 56 57 Raises: 58 - Exception: If the pixel format of the texture is not MTLPixelFormatBGRA8Unorm or MTLPixelFormatRGBA8Unorm. 59 - Exception: If the provided buffer is not big enough. 60 """ 61 if ( 62 texture.pixelFormat() != Metal.MTLPixelFormatBGRA8Unorm 63 and texture.pixelFormat() != Metal.MTLPixelFormatRGBA8Unorm 64 ): 65 raise Exception("Not correct pixel format (expected MTLPixelFormatBGRA8Unorm or MTLPixelFormatRGBA8Unorm)") 66 67 bytes_per_row = texture.width() * 4 68 bytes_per_image = bytes_per_row * texture.height() 69 mipmap_level = 0 70 slice_number = 0 71 region = Metal.MTLRegionMake2D(0, 0, texture.width(), texture.height()) 72 73 if buffer is None: 74 buffer = ctypes.create_string_buffer(bytes_per_image) 75 76 if len(buffer) != bytes_per_image: 77 raise Exception(f"Buffer is not big enough (expected: {bytes_per_image}, actual: {len(buffer)})") 78 79 texture.getBytes_bytesPerRow_bytesPerImage_fromRegion_mipmapLevel_slice_( 80 buffer, bytes_per_row, bytes_per_image, region, mipmap_level, slice_number 81 ) 82 83 raw_bytes = bytes(buffer.raw) 84 return raw_bytes
def
create_mtl_texture(device: Any, width: int, height: int, pixel_format: int = 70) -> Any:
8def create_mtl_texture(device: Any, width: int, height: int, pixel_format: int = Metal.MTLPixelFormatRGBA8Unorm) -> Any: 9 """ 10 Create a Metal texture with the specified parameters. 11 12 Parameters: 13 - device (Any): The Metal device. 14 - width (int): The width of the texture. 15 - height (int): The height of the texture. 16 - pixel_format (int): The pixel format of the texture (default: MTLPixelFormatRGBA8Unorm). 17 18 Returns: 19 - Any: The created Metal texture. 20 """ 21 texture_descriptor = Metal.MTLTextureDescriptor.texture2DDescriptorWithPixelFormat_width_height_mipmapped_( 22 pixel_format, width, height, False 23 ) 24 25 return device.newTextureWithDescriptor_(texture_descriptor)
Create a Metal texture with the specified parameters.
Parameters:
- device (Any): The Metal device.
- width (int): The width of the texture.
- height (int): The height of the texture.
- pixel_format (int): The pixel format of the texture (default: MTLPixelFormatRGBA8Unorm).
Returns:
- Any: The created Metal texture.
def
copy_bytes_to_mtl_texture(data: bytes, texture: Any):
28def copy_bytes_to_mtl_texture(data: bytes, texture: Any): 29 """ 30 Copy pixel data from a bytes object to a Metal texture. 31 32 Parameters: 33 - data (bytes): The pixel data as bytes. 34 - texture (Any): The target Metal texture to copy the pixel data into. 35 """ 36 region = Metal.MTLRegion((0, 0, 0), (texture.width(), texture.height(), 1)) 37 bytes_per_row = texture.width() * 4 38 39 texture.replaceRegion_mipmapLevel_withBytes_bytesPerRow_( 40 region, 41 0, # mipmapLevel 42 data, 43 bytes_per_row, 44 )
Copy pixel data from a bytes object to a Metal texture.
Parameters:
- data (bytes): The pixel data as bytes.
- texture (Any): The target Metal texture to copy the pixel data into.
def
copy_mtl_texture_to_bytes(texture: Any, buffer: Any | None = None) -> bytes:
47def copy_mtl_texture_to_bytes(texture: Any, buffer: Optional[Any] = None) -> bytes: 48 """ 49 Copy pixel data from a Metal texture to a bytes object. 50 51 Parameters: 52 - texture (Any): The source Metal texture to copy pixel data from. 53 - buffer (Optional[Any]): The buffer to store the result. If None, a new buffer will be created. 54 55 Returns: 56 - bytes: The resulting pixel data as bytes. 57 58 Raises: 59 - Exception: If the pixel format of the texture is not MTLPixelFormatBGRA8Unorm or MTLPixelFormatRGBA8Unorm. 60 - Exception: If the provided buffer is not big enough. 61 """ 62 if ( 63 texture.pixelFormat() != Metal.MTLPixelFormatBGRA8Unorm 64 and texture.pixelFormat() != Metal.MTLPixelFormatRGBA8Unorm 65 ): 66 raise Exception("Not correct pixel format (expected MTLPixelFormatBGRA8Unorm or MTLPixelFormatRGBA8Unorm)") 67 68 bytes_per_row = texture.width() * 4 69 bytes_per_image = bytes_per_row * texture.height() 70 mipmap_level = 0 71 slice_number = 0 72 region = Metal.MTLRegionMake2D(0, 0, texture.width(), texture.height()) 73 74 if buffer is None: 75 buffer = ctypes.create_string_buffer(bytes_per_image) 76 77 if len(buffer) != bytes_per_image: 78 raise Exception(f"Buffer is not big enough (expected: {bytes_per_image}, actual: {len(buffer)})") 79 80 texture.getBytes_bytesPerRow_bytesPerImage_fromRegion_mipmapLevel_slice_( 81 buffer, bytes_per_row, bytes_per_image, region, mipmap_level, slice_number 82 ) 83 84 raw_bytes = bytes(buffer.raw) 85 return raw_bytes
Copy pixel data from a Metal texture to a bytes object.
Parameters:
- texture (Any): The source Metal texture to copy pixel data from.
- buffer (Optional[Any]): The buffer to store the result. If None, a new buffer will be created.
Returns:
- bytes: The resulting pixel data as bytes.
Raises:
- Exception: If the pixel format of the texture is not MTLPixelFormatBGRA8Unorm or MTLPixelFormatRGBA8Unorm.
- Exception: If the provided buffer is not big enough.