Skip to content

Commit

Permalink
Mark shaders with missing features (#29)
Browse files Browse the repository at this point in the history
* add complete attribute

* add quotation marks in title
  • Loading branch information
Vipitis authored May 7, 2024
1 parent 11070dc commit eb14d38
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 3 additions & 1 deletion examples/shadertoy_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@
"""


shader = Shadertoy(image_code, common=common_code, resolution=(800, 450))
shader = Shadertoy(
image_code, common=common_code, resolution=(800, 450), complete=False
)

if __name__ == "__main__":
shader.show()
4 changes: 2 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_shadertoy_from_id(api_available):
# shadertoy source: https://www.shadertoy.com/view/l3fXWN by Vipitis
shader = Shadertoy.from_id("l3fXWN")

assert shader.title == "API test for CI by jakel101"
assert shader.title == '"API test for CI" by jakel101'
assert shader.shader_type == "glsl"
assert shader.shader_code.startswith("//Confirm API working!")
assert shader.common.startswith("//Common pass loaded!")
Expand All @@ -55,7 +55,7 @@ def test_shadertoy_from_id_without_cache(api_available):
# shadertoy source: https://www.shadertoy.com/view/l3fXWN by Vipitis
shader = Shadertoy.from_id("l3fXWN", use_cache=False)

assert shader.title == "API test for CI by jakel101"
assert shader.title == '"API test for CI" by jakel101'
assert shader.shader_type == "glsl"
assert shader.shader_code.startswith("//Confirm API working!")
assert shader.common.startswith("//Common pass loaded!")
Expand Down
19 changes: 11 additions & 8 deletions wgpu_shadertoy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import requests
from PIL import Image
from wgpu import logger

from .inputs import ShadertoyChannel

Expand Down Expand Up @@ -60,8 +59,10 @@ def _download_media_channels(inputs: list, use_cache=True):
media_url = "https://www.shadertoy.com"
channels = {}
cache_dir = _get_cache_dir("media")
complete = True
for inp in inputs:
if inp["ctype"] != "texture":
complete = False
continue # TODO: support other media types

cache_path = os.path.join(cache_dir, inp["src"].split("/")[-1])
Expand All @@ -81,7 +82,7 @@ def _download_media_channels(inputs: list, use_cache=True):

channel = ShadertoyChannel(img, kind="texture", **inp["sampler"])
channels[inp["channel"]] = channel
return list(channels.values())
return list(channels.values()), complete


def _save_json(data, path):
Expand Down Expand Up @@ -131,6 +132,7 @@ def shader_args_from_json(dict_or_path, **kwargs) -> dict:
main_image_code = ""
common_code = ""
inputs = []
complete = True
if "Shader" not in shader_data:
raise ValueError(
"shader_data must have a 'Shader' key, following Shadertoy export format."
Expand All @@ -139,22 +141,23 @@ def shader_args_from_json(dict_or_path, **kwargs) -> dict:
if r_pass["type"] == "image":
main_image_code = r_pass["code"]
if r_pass["inputs"] is not []:
inputs = _download_media_channels(r_pass["inputs"], use_cache=use_cache)
inputs, inputs_complete = _download_media_channels(
r_pass["inputs"], use_cache=use_cache
)
elif r_pass["type"] == "common":
common_code = r_pass["code"]
else:
# TODO should be a warning and not verbose!
logger.warn(
f"renderpass of type {r_pass['type']} not yet supported, will be omitted."
)
title = f'{shader_data["Shader"]["info"]["name"]} by {shader_data["Shader"]["info"]["username"]}'
complete = False
complete = complete and inputs_complete
title = f'"{shader_data["Shader"]["info"]["name"]}" by {shader_data["Shader"]["info"]["username"]}'

shader_args = {
"shader_code": main_image_code,
"common": common_code,
"shader_type": "glsl",
"inputs": inputs,
"title": title,
"complete": complete,
**kwargs,
}
return shader_args
6 changes: 6 additions & 0 deletions wgpu_shadertoy/shadertoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ class Shadertoy:
offscreen (bool): Whether to render offscreen. Default is False.
inputs (list): A list of :class:`ShadertoyChannel` objects. Supports up to 4 inputs. Defaults to sampling a black texture.
title (str): The title of the window. Defaults to "Shadertoy".
complete (bool): Whether the shader is complete. Unsupported renderpasses or inputs will set this to False. Default is True.
The shader code must contain a entry point function:
Expand Down Expand Up @@ -309,6 +310,7 @@ def __init__(
offscreen=None,
inputs=[],
title: str = "Shadertoy",
complete: bool = True,
) -> None:
self._uniform_data = UniformArray(
("mouse", "f", 4),
Expand Down Expand Up @@ -337,6 +339,10 @@ def __init__(
self.inputs = inputs
self.inputs.extend([ShadertoyChannel() for _ in range(4 - len(inputs))])
self.title = title
self.complete = complete

if not self.complete:
self.title += " (incomplete)"

self._prepare_render()
self._bind_events()
Expand Down

0 comments on commit eb14d38

Please sign in to comment.