Skip to content

Instantly share code, notes, and snippets.

@player1537
Last active August 17, 2023 14:27
Show Gist options
  • Save player1537/877548c6b65d506c8f2bc525e6c7480f to your computer and use it in GitHub Desktop.
Save player1537/877548c6b65d506c8f2bc525e6c7480f to your computer and use it in GitHub Desktop.
"""
"""
__all__ = [
'maybe_download',
'maybe_extract',
'maybe_init',
]
#--- Utility: Automatic Imports
@lambda f: f()
class auto:
def __getattr__(self, name: str):
import importlib
module = importlib.import_module(name)
setattr(self, name, module)
return module
#--- Main
OSPRAY_VERSION = (
'2.12.0'
)
def maybe_download(
*,
version: str=OSPRAY_VERSION,
) -> auto.typing.Annotated[auto.pathlib.Path, 'tarball']:
# Thanks https://stackoverflow.com/a/44378512
#> from urllib.request import urlretrieve
#> url = 'http://mirror.pnl.gov/releases/16.04.2/ubuntu-16.04.2-desktop-amd64.iso'
src = f"""https://github.com/ospray/ospray/releases/download/v{version}/ospray-{version}.x86_64.linux.tar.gz"""
#> dst = 'ubuntu-16.04.2-desktop-amd64.iso'
dst = auto.pathlib.Path.cwd() / 'tmp' / f"""ospray-{version}.tar.gz"""
#> urlretrieve(url, dst)
if not dst.exists():
print(f"""Download {src} to {dst}""")
auto.urllib.request.urlretrieve(src, str(dst))
else:
print(f"""Already downloaded to {dst}""")
return dst
def maybe_extract(
*,
version: str=OSPRAY_VERSION,
tarball: Path,
) -> auto.typing.Annotated[auto.pathlib.Path, 'prefix']:
src = tarball
dst = auto.pathlib.Path.cwd() / 'tmp' / f"""ospray-{version}"""
if dst.exists():
print(f"""Already extracted {src} to {dst}""")
else:
with auto.tempfile.TemporaryDirectory() as tmp:
print(f"""Extract {src} to {tmp}""")
tmp = auto.pathlib.Path(tmp)
with auto.tarfile.open(src, 'r') as tar:
tar.extractall(tmp)
src = tmp / f"""ospray-{version}.x86_64.linux"""
print(f"""Move {src} to {dst}""")
auto.shutil.move(src, dst)
return dst
def __getattr__(
name: Literal['lib'],
) -> auto.typing.Annotated[auto.ctypes.CDLL, 'lib']:
if name == 'lib':
tarball = maybe_download()
prefix = maybe_extract()
lib = maybe_init()
return lib
raise NotImplementedError(f"Only getattr(ospray, 'lib') is defined")
_lib = None
def maybe_init(
*,
prefix: auto.pathlib.Path,
) -> auto.typing.Annotated[auto.ctypes.CDLL, 'lib']:
global _lib
if _lib is not None:
return _lib
_lib = lib = auto.ctypes.CDLL(
name=(
prefix / 'lib' / 'libospray.so'
),
mode=auto.os.RTLD_LOCAL | auto.os.RTLD_DEEPBIND,
)
lib.ospInit.argtypes = [
auto.ctypes.POINTER(auto.ctypes.c_int),
auto.ctypes.POINTER(auto.ctypes.c_char_p),
]
lib.ospInit.restype = auto.ctypes.c_uint32
error = lib.ospInit(None, None)
assert error == 0, \
f"""Error during ospInit: {error=!r}"""
if not hasattr(lib, 'OSPObject'):
lib.OSPObject = auto.ctypes.POINTER(type('OSPObject', (auto.ctypes.Structure,), {
'_fields': [],
}))
for name in auto.re.findall(r'\w+', r'''
*OSPCamera, *OSPData, OSPFrameBuffer, *OSPFuture, *OSPGeometricModel,
**OSPGeometry, *OSPGroup, OSPImageOperation, *OSPInstance, *OSPLight,
**OSPMaterial, *, OSPRenderer, *OSPTexture,
**OSPTransferFunction, *OSPVolume, OSPVolumetricModel, *OSPWorld;
'''):
# setattr(lib, name, auto.ctypes.POINTER(type(name, (OSPObject,), {
# '_fields_': [],
# })))
setattr(lib, name, lib.OSPObject)
lib.OSPError = type('OSPError', (auto.ctypes.c_uint32,), dict(
OSP_NO_ERROR=0, # no error occurred
OSP_UNKNOWN_ERROR=1, # an unknown error occurred
OSP_INVALID_ARGUMENT=2, # an invalid argument was specified
OSP_INVALID_OPERATION=3, # the operation is not allowed for the specified object
OSP_OUT_OF_MEMORY=4, # there is not enough memory to execute the command
OSP_UNSUPPORTED_CPU=5, # the CPU is not supported (minimum ISA is SSE4.1 on x86_64 and NEON on ARM64)
OSP_VERSION_MISMATCH=6, # a module could not be loaded due to mismatching version
))
lib.OSPDataType = auto.ctypes.c_uint32
lib.OSPFrameBufferFormat = auto.ctypes.c_uint32
lib.OSPFrameBufferChannel = auto.ctypes.c_uint32
lib.OSPTextureFormat = auto.ctypes.c_uint32
lib.OSPTextureFilter = auto.ctypes.c_uint32
for name, value in dict(
# highest bit to represent objects/handles
OSP_OBJECT = 0x8000000,
# object subtypes
OSP_DATA = 0x8000000 + 100 + 0,
OSP_CAMERA = 0x8000000 + 100 + 1,
OSP_FRAMEBUFFER = 0x8000000 + 100 + 2,
OSP_FUTURE = 0x8000000 + 100 + 3,
OSP_GEOMETRIC_MODEL = 0x8000000 + 100 + 4,
OSP_GEOMETRY = 0x8000000 + 100 + 5,
OSP_GROUP = 0x8000000 + 100 + 6,
OSP_IMAGE_OPERATION = 0x8000000 + 100 + 7,
OSP_INSTANCE = 0x8000000 + 100 + 8,
OSP_LIGHT = 0x8000000 + 100 + 9,
OSP_MATERIAL = 0x8000000 + 100 + 10,
OSP_RENDERER = 0x8000000 + 100 + 11,
OSP_TEXTURE = 0x8000000 + 100 + 12,
OSP_TRANSFER_FUNCTION = 0x8000000 + 100 + 13,
OSP_VOLUME = 0x8000000 + 100 + 14,
OSP_VOLUMETRIC_MODEL = 0x8000000 + 100 + 15,
OSP_WORLD = 0x8000000 + 100 + 16,
OSP_UCHAR = 2500,
OSP_VEC2UC = 2501,
OSP_VEC3UC = 2502,
OSP_VEC4UC = 2503,
OSP_VEC4UI = 4500 + 3,
OSP_VEC2F = 6000 + 1,
OSP_VEC3F = 6000 + 2,
OSP_VEC4F = 6000 + 3,
OSP_LINEAR2F = 12000 + 0,
OSP_LINEAR3F = 12000 + 1,
OSP_AFFINE2F = 12000 + 2,
OSP_AFFINE3F = 12000 + 3,
OSP_FB_NONE = 0,
OSP_FB_RGBA8 = 1,
OSP_FB_SRGBA = 2,
OSP_FB_RGBA32F = 3,
OSP_FB_COLOR = 1 << 0,
OSP_FB_DEPTH = 1 << 1,
OSP_FB_ACCUM = 1 << 2,
OSP_FB_VARIANCE = 1 << 3,
OSP_TEXTURE_RGBA8 = 0,
OSP_TEXTURE_SRGBA = 1,
OSP_TEXTURE_RGBA32F = 2,
OSP_TEXTURE_RGB8 = 3,
OSP_TEXTURE_SRGB = 4,
OSP_TEXTURE_RGB32F = 5,
OSP_TEXTURE_R8 = 6,
OSP_TEXTURE_R32F = 7,
OSP_TEXTURE_L8 = 8,
OSP_TEXTURE_RA8 = 9,
OSP_TEXTURE_LA8 = 10,
OSP_TEXTURE_RGBA16 = 11,
OSP_TEXTURE_RGB16 = 12,
OSP_TEXTURE_RA16 = 13,
OSP_TEXTURE_R16 = 14,
OSP_TEXTURE_FILTER_BILINEAR = 0,
OSP_TEXTURE_FILTER_NEAREST = 1,
).items():
setattr(lib, name, value)
lib.ospInit.argtypes = [
auto.ctypes.POINTER(auto.ctypes.c_int),
auto.ctypes.POINTER(auto.ctypes.c_char_p),
]
lib.ospInit.restype = lib.OSPError
lib.ospCommit.argtypes = [lib.OSPObject]
lib.ospCommit.restype = None
lib.ospRetain.argtypes = [lib.OSPObject]
lib.ospRetain.restype = None
lib.ospRelease.argtypes = [lib.OSPObject]
lib.ospRelease.restype = None
lib.ospSetParam.argtypes = [lib.OSPObject, auto.ctypes.c_char_p, lib.OSPDataType, auto.ctypes.c_void_p]
lib.ospSetParam.restype = None
#--- Data
lib.ospNewSharedData.argtypes = [
auto.ctypes.c_void_p,
lib.OSPDataType,
auto.ctypes.c_uint64, auto.ctypes.c_int64,
auto.ctypes.c_uint64, auto.ctypes.c_int64,
auto.ctypes.c_uint64, auto.ctypes.c_int64,
]
lib.ospNewSharedData.restype = lib.OSPData
lib.ospNewData.argtypes = [
lib.OSPDataType,
auto.ctypes.c_uint64,
auto.ctypes.c_uint64,
auto.ctypes.c_uint64,
]
lib.ospNewData.restype = lib.OSPData
lib.ospCopyData.argtypes = [
lib.OSPData,
lib.OSPData,
auto.ctypes.c_uint64,
auto.ctypes.c_uint64,
auto.ctypes.c_uint64,
]
lib.ospCopyData.restype = None
#--- Constructors
lib.ospNewWorld.argtypes = []
lib.ospNewWorld.restype = lib.OSPWorld
lib.ospNewInstance.argtypes = [lib.OSPGroup]
lib.ospNewInstance.restype = lib.OSPInstance
lib.ospNewGroup.argtypes = []
lib.ospNewGroup.restype = lib.OSPGroup
lib.ospNewGeometricModel.argtypes = [lib.OSPGeometry]
lib.ospNewGeometricModel.restype = lib.OSPGeometricModel
lib.ospNewGeometry.argtypes = [auto.ctypes.c_char_p]
lib.ospNewGeometry.restype = lib.OSPGeometry
lib.ospNewMaterial.argtypes = [auto.ctypes.c_void_p, auto.ctypes.c_char_p]
lib.ospNewMaterial.restype = lib.OSPMaterial
lib.ospNewRenderer.argtypes = [auto.ctypes.c_char_p]
lib.ospNewRenderer.restype = lib.OSPRenderer
lib.ospNewCamera.argtypes = [auto.ctypes.c_char_p]
lib.ospNewCamera.restype = lib.OSPCamera
lib.ospNewTexture.argtypes = [auto.ctypes.c_char_p]
lib.ospNewTexture.restype = lib.OSPTexture
lib.ospNewFrameBuffer.argtypes = [
auto.ctypes.c_int, auto.ctypes.c_int,
lib.OSPFrameBufferFormat,
lib.OSPFrameBufferChannel,
]
lib.ospNewFrameBuffer.restype = lib.OSPFrameBuffer
lib.ospMapFrameBuffer.argtypes = [lib.OSPFrameBuffer, lib.OSPFrameBufferChannel]
lib.ospMapFrameBuffer.restype = auto.ctypes.c_void_p
lib.ospUnmapFrameBuffer.argtypes = [auto.ctypes.c_void_p, lib.OSPFrameBuffer]
lib.ospUnmapFrameBuffer.restype = None
lib.OSPBounds = type('OSPBounds', (auto.ctypes.Structure,), {
'_fields_': [
('xlo', auto.ctypes.c_float),
('ylo', auto.ctypes.c_float),
('zlo', auto.ctypes.c_float),
('xhi', auto.ctypes.c_float),
('yhi', auto.ctypes.c_float),
('zhi', auto.ctypes.c_float),
],
})
lib.ospGetBounds.argtypes = [lib.OSPObject]
lib.ospGetBounds.restype = lib.OSPBounds
#--- Utilities
lib.ospNewSharedData1D.argtypes = [
auto.ctypes.c_void_p, lib.OSPDataType,
auto.ctypes.c_uint64,
]
lib.ospNewSharedData1D.restype = lib.OSPData
lib.ospNewSharedData2D.argtypes = [
auto.ctypes.c_void_p, lib.OSPDataType,
auto.ctypes.c_uint64, auto.ctypes.c_uint64,
]
lib.ospNewSharedData2D.restype = lib.OSPData
lib.ospNewSharedData3D.argtypes = [
auto.ctypes.c_void_p, lib.OSPDataType,
auto.ctypes.c_uint64, auto.ctypes.c_uint64, auto.ctypes.c_uint64,
]
lib.ospNewSharedData3D.restype = lib.OSPData
lib.ospNewData1D.argtypes = [
lib.OSPDataType,
auto.ctypes.c_uint64,
]
lib.ospNewData1D.restype = lib.OSPData
lib.ospNewData2D.argtypes = [
lib.OSPDataType,
auto.ctypes.c_uint64, auto.ctypes.c_uint64,
]
lib.ospNewData2D.restype = lib.OSPData
lib.ospCopyData1D.argtypes = [
lib.OSPData, lib.OSPData,
auto.ctypes.c_uint64,
]
lib.ospCopyData1D.restype = None
lib.ospCopyData2D.argtypes = [
lib.OSPData, lib.OSPData,
auto.ctypes.c_uint64, auto.ctypes.c_uint64,
]
lib.ospCopyData2D.restype = None
def __define_setter(suffix: str, /, *argtypes: list, n=1):
assert len(argtypes) > 0
getattr(lib, f'ospSet{suffix}').argtypes = [
lib.OSPObject, auto.ctypes.c_char_p,
*(argtypes * n),
]
getattr(lib, f'ospSet{suffix}').restype = None
__define_setter('String', auto.ctypes.c_char_p)
__define_setter('Object', lib.OSPObject)
__define_setter('Bool', auto.ctypes.c_int)
__define_setter('Float', auto.ctypes.c_float)
__define_setter('Int', auto.ctypes.c_int)
__define_setter('Vec2f', auto.ctypes.c_float, n=2)
__define_setter('Vec3f', auto.ctypes.c_float, n=3)
__define_setter('Vec4f', auto.ctypes.c_float, n=4)
__define_setter('Vec2i', auto.ctypes.c_int, n=2)
__define_setter('Vec3i', auto.ctypes.c_int, n=3)
__define_setter('Vec4i', auto.ctypes.c_int, n=4)
lib.ospSetObjectAsData.argtypes = [
lib.OSPObject, auto.ctypes.c_char_p,
lib.OSPDataType, lib.OSPObject,
]
lib.ospRenderFrameBlocking.argtypes = [lib.OSPFrameBuffer, lib.OSPRenderer, lib.OSPCamera, lib.OSPWorld]
lib.ospRenderFrameBlocking.restype = auto.ctypes.c_float
return _lib
[project]
name = "ospray"
version = "0.1.0"
description = "Lightweight wrapper around Intel OSPRay"
readme = "README.md"
authors = [{name = "https://github.com/player1537"}]
license = {text = "wtfpl"}
classifiers = [
"Development Status :: 4 - Beta",
"Topic :: Scientific/Engineering :: Visualization",
"Topic :: Multimedia :: Graphics :: 3D Rendering",
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment