Skip to content

Commit

Permalink
Merge pull request #2 from aforren1/dxt5
Browse files Browse the repository at this point in the history
 - DXT5 compression
   - OpenMP support included
   - ModernGL does not fully support compressed textures yet, but a hacked test worked fine
 - Vendor stb and qoi for simplicity
 - Measure .patlas file sizes during testing
 - CI updates & improvements (cancel running builds when newer available)
  • Loading branch information
aforren1 authored Dec 28, 2021
2 parents cb7de43 + b12db62 commit cc15541
Show file tree
Hide file tree
Showing 15 changed files with 10,117 additions and 58 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ on: [push]

env:
CIBW_SKIP: pp* *-manylinux_i686 *-musllinux_*
CIBW_BEFORE_BUILD: git submodule update --init --recursive
CIBW_BEFORE_BUILD_MACOS: $CIBW_BEFORE_BUILD && brew install libomp
CIBW_BEFORE_BUILD_MACOS: brew install libomp # necessary for wheel building, even when not enabled?
CIBW_TEST_COMMAND: python {project}/test.py
CIBW_ARCHS_LINUX: auto aarch64
# CIBW_ENVIRONMENT: OMP=1

jobs:
cancel_previous:
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

build_wheels:
name: Build wheel on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -59,7 +66,6 @@ jobs:

- name: Build source
run: |
git submodule update --init --recursive
python -m pip install setuptools wheel Cython>=3.0a9
python setup.py sdist --formats=zip
- uses: actions/upload-artifact@v2
Expand Down
6 changes: 0 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
[submodule "stb"]
path = stb
url = https://github.com/nothings/stb
[submodule "qoi"]
path = qoi
url = https://github.com/phoboslab/qoi
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include patlas.pyx
include stb/stb_image.h
include stb/stb_rect_pack.h
include stb/stb_dxt.h
include qoi/qoi.h
global-include LICENSE
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ap.pack(glob('images/*.png')) # list of images
ap.pack(['images/image.jpg']) # can call multiple times (packing quality may suffer)

ap.atlas # memoryview of RGBA texture
ap.metadata # dictionary of image locations
ap.metadata # dictionary of image locations and image format

ap.save('atlas') # serialize as custom .patlas file

Expand All @@ -25,9 +25,10 @@ See [demo.py](https://github.com/aforren1/patlas/blob/main/demo.py) for example

Features/limitations:

- Uses `stb_image` and `stb_rect_pack` from [stb](https://github.com/nothings/stb)
- Uses `stb_image`, `stb_rect_pack`, and `stb_dxt` from [stb](https://github.com/nothings/stb)
- Can import any image format `stb_image` can (see [here](https://github.com/nothings/stb/blob/5ba0baaa269b3fd681828e0e3b3ac0f1472eaf40/stb_image.h#L23))
- Only square RGBA textures (currently)
- Optional DXT5/BC3(?) compression
- Optional OpenMP support (disabled by default to reduce wheel size) can substantially reduce runtime. To enable, build from source with `OMP=1` set in the environment, e.g. `OMP=1 pip install patlas --no-binary patlas`
- On Windows, should "just work"?
- MacOS may need extra packages, e.g. `libomp` from brew
Expand Down
16 changes: 9 additions & 7 deletions demo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import moderngl as mgl
import glfw
import numpy as np
from patlas import AtlasPacker
from patlas import AtlasPacker, TextureFormat

image_vert = """
#version 330
Expand All @@ -18,12 +18,12 @@

image_frag = """
#version 330
uniform sampler2D texture;
uniform sampler2D tex;
in vec2 v_texcoord;
out vec4 f_color;
void main()
{
f_color = texture2D(texture, v_texcoord);
f_color = texture(tex, v_texcoord);
}
"""

Expand All @@ -48,24 +48,26 @@

prog = ctx.program(vertex_shader=image_vert, fragment_shader=image_frag)

ap = AtlasPacker(2048, pad=1)
ap = AtlasPacker(2048, pad=1)#, texture_format=TextureFormat.DXT5)

ap.pack(['images/alex.png', 'images/kazoo.jpg'])

atlas = ap.atlas
tex = ctx.texture(atlas.shape[0:2], atlas.shape[2], atlas)
print(ap.metadata)
tex = ctx.texture(atlas.shape[0:2], atlas.shape[2], atlas,
internal_format=ap.metadata['texture_format'])

vbo = np.empty(4, dtype=[('vertices', np.float32, 2), ('texcoord', np.float32, 2)])
vbo['vertices'] = [(-1, -1), (-1, -0.25), (-0.25, -1), (-0.25, -0.25)]
vbo['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]
buf = ctx.buffer(vbo)
vao = ctx.vertex_array(prog, buf, 'vertices', 'texcoord')

a = ap.metadata['alex']
a = ap.metadata['images']['alex']
atex = np.array([(a['u0'], a['v0']), (a['u0'], a['v1']),
(a['u1'], a['v0']), (a['u1'], a['v1'])],
dtype='f4')
k = ap.metadata['kazoo']
k = ap.metadata['images']['kazoo']
ktex = np.array([(k['u0'], k['v0']), (k['u0'], k['v1']),
(k['u1'], k['v0']), (k['u1'], k['v1'])],
dtype='f4')
Expand Down
37 changes: 37 additions & 0 deletions deps/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Sean Barrett
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit cc15541

Please sign in to comment.