Skip to content

Commit

Permalink
add geometry checks for available shaders in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishavlin committed Dec 13, 2024
1 parent 8e8936c commit 6eb21f1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 21 deletions.
48 changes: 29 additions & 19 deletions yt_idv/scene_components/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from yt_idv.opengl_support import TransferFunctionTexture
from yt_idv.scene_components.base_component import SceneComponent
from yt_idv.scene_data.block_collection import BlockCollection
from yt_idv.shader_objects import component_shaders
from yt_idv.shader_objects import component_shaders, get_shader_combos


class BlockRendering(SceneComponent):
Expand All @@ -34,34 +34,40 @@ class BlockRendering(SceneComponent):

def render_gui(self, imgui, renderer, scene):
changed = super().render_gui(imgui, renderer, scene)

max_sf = 50.0 if self._yt_geom_str == "spherical" else 20.0
_, sample_factor = imgui.slider_float(
"Sample Factor", self.sample_factor, 1.0, 20.0
"Sample Factor", self.sample_factor, 1.0, max_sf
)
if _:
self.sample_factor = sample_factor
# Now, shaders
shader_combos = list(sorted(component_shaders[self.name]))
valid_shaders = get_shader_combos(
self.name, coord_system=self.data._yt_geom_str
)
descriptions = [
component_shaders[self.name][_]["description"] for _ in shader_combos
component_shaders[self.name][_]["description"] for _ in valid_shaders
]
selected = shader_combos.index(self.render_method)
selected = valid_shaders.index(self.render_method)
_, shader_ind = imgui.listbox("Shader", selected, descriptions)
if _:
self.render_method = shader_combos[shader_ind]
self.render_method = valid_shaders[shader_ind]
changed = changed or _
if imgui.button("Add Block Outline"):
from ..scene_annotations.block_outline import BlockOutline

block_outline = BlockOutline(data=self.data)
scene.annotations.append(block_outline)
if imgui.button("Add Grid Outline"):
from ..scene_annotations.grid_outlines import GridOutlines
from ..scene_data.grid_positions import GridPositions

grids = self.data.data_source.ds.index.grids.tolist()
gp = GridPositions(grid_list=grids)
scene.data_objects.append(gp)
scene.components.append(GridOutlines(data=gp))
if self.data._yt_geom_str == "cartesian":
# the following only work for cartesian data at present
if imgui.button("Add Block Outline"):
from ..scene_annotations.block_outline import BlockOutline

block_outline = BlockOutline(data=self.data)
scene.annotations.append(block_outline)
if imgui.button("Add Grid Outline"):
from ..scene_annotations.grid_outlines import GridOutlines
from ..scene_data.grid_positions import GridPositions

grids = self.data.data_source.ds.index.grids.tolist()
gp = GridPositions(grid_list=grids)
scene.data_objects.append(gp)
scene.components.append(GridOutlines(data=gp))
if self.render_method == "transfer_function":
# Now for the transfer function stuff
imgui.image_button(
Expand Down Expand Up @@ -159,3 +165,7 @@ def _set_uniforms(self, scene, shader_program):
shader_program._set_uniform("tf_log", float(self.tf_log))
shader_program._set_uniform("slice_normal", np.array(self.slice_normal))
shader_program._set_uniform("slice_position", np.array(self.slice_position))

@property
def _yt_geom_str(self):
return self.data._yt_geom_str
4 changes: 2 additions & 2 deletions yt_idv/scene_components/octree_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from yt_idv.opengl_support import TransferFunctionTexture
from yt_idv.scene_components.base_component import SceneComponent
from yt_idv.scene_data.octree_block_collection import OctreeBlockCollection
from yt_idv.shader_objects import component_shaders
from yt_idv.shader_objects import component_shaders, get_shader_combos


class OctreeBlockRendering(SceneComponent):
Expand Down Expand Up @@ -37,7 +37,7 @@ def render_gui(self, imgui, renderer, scene):
if _:
self.sample_factor = sample_factor
# Now, shaders
shader_combos = list(sorted(component_shaders[self.name]))
shader_combos = get_shader_combos(self.name)
descriptions = [
component_shaders[self.name][_]["description"] for _ in shader_combos
]
Expand Down
16 changes: 16 additions & 0 deletions yt_idv/shader_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,19 @@ def reset(self, shader_type: Optional[str] = None):
default_shader_combos.update(
{_: component_shaders[_].pop("default_value") for _ in component_shaders}
)


def get_shader_combos(component_name, coord_system="cartesian"):
shader_combos = list(sorted(component_shaders[component_name]))
if coord_system == "cartesian":
return shader_combos

valid_shader_combos = []
for shader_name in shader_combos:
shader = component_shaders[component_name][shader_name]
if (
"coordinate_systems" in shader
and coord_system in shader["coordinate_systems"]
):
valid_shader_combos.append(shader_name)
return valid_shader_combos
4 changes: 4 additions & 0 deletions yt_idv/shaders/shaderlist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,23 @@ component_shaders:
first_fragment: max_intensity
second_vertex: passthrough
second_fragment: apply_colormap
coordinate_systems: [cartesian, spherical]
projection:
description: Projective integration
first_vertex: grid_position
first_geometry: grid_expand
first_fragment: projection
second_vertex: passthrough
second_fragment: apply_colormap
coordinate_systems: [cartesian, spherical]
transfer_function:
description: Color transfer function
first_vertex: grid_position
first_geometry: grid_expand
first_fragment: transfer_function
second_vertex: passthrough
second_fragment: passthrough
coordinate_systems: [cartesian, spherical]
isocontours:
description: Isocontours
first_vertex: grid_position
Expand All @@ -243,6 +246,7 @@ component_shaders:
first_fragment: constant
second_vertex: passthrough
second_fragment: apply_colormap
coordinate_systems: [cartesian, spherical]
octree_block_rendering:
default_value: max_intensity
max_intensity:
Expand Down
23 changes: 23 additions & 0 deletions yt_idv/tests/test_component_shaders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from yt_idv.shader_objects import component_shaders, get_shader_combos


@pytest.mark.parametrize("render_name", component_shaders.keys())
def test_get_shader_combos_all_components(render_name):
# default call to get_shader_combos should return all shaders
expected = component_shaders[render_name]
actual = get_shader_combos(render_name)
assert set(expected) == set(actual)


def test_get_shader_combos_coord():
# only block_rendering supports non-cartesian for now, check that the supported
# shaders are returned as expected.
shaders = get_shader_combos("block_rendering", coord_system="spherical")
not_supported = ["isocontours", "slice"]
assert all(s not in shaders for s in not_supported)
supported = ["max_intensity", "projection", "transfer_function", "constant"]
for s in supported:
assert s in shaders
assert all(s in shaders for s in supported)

0 comments on commit 6eb21f1

Please sign in to comment.