yt-project / yt_idv

Interactive volume rendering for yt
Other
9 stars 6 forks source link

First attempt at preprocessor directives #70

Closed matthewturk closed 1 year ago

matthewturk commented 1 year ago

This should be enough to get the shader programs to accept preprocessor directives. It's not quite ready to go yet, because right now the shader objects themselves are created inside the shader program, and I'm not passing the full set of preprocessor directives down.

How it works right now is that base_component.py has the function _recompile_shader. This deletes the shaders and sets _program1_invalid and _program2_invalid to True. The next time the call gets made to program1, it is a property that calls:

self._program1 = ShaderProgram(
    self.vertex_shader, self.fragment_shader, self.geometry_shader
)

We'll need to include in this a set of parameters that are defined on the base component. I think this could be a property defined by each subclass that translates properties/traits into strings.

matthewturk commented 1 year ago

One thing I do need to keep an eye on is making sure that #version is still the very first line.

matthewturk commented 1 year ago

OK, I fixed that up by moving around where we include the preprocessor definitions. I tested that it worked with this diff as well:

diff --git a/examples/amr_volume_rendering.py b/examples/amr_volume_rendering.py
index 17fd8ea..c0b1619 100644
--- a/examples/amr_volume_rendering.py
+++ b/examples/amr_volume_rendering.py
@@ -6,4 +6,5 @@ ds = yt.load_sample("IsolatedGalaxy")

 rc = yt_idv.render_context(height=800, width=800, gui=True)
 sg = rc.add_scene(ds, "density", no_ghost=True)
+sg.add_text("Hi there")
 rc.run()
diff --git a/yt_idv/shader_objects.py b/yt_idv/shader_objects.py
index 5cb4cd1..bb5473e 100644
--- a/yt_idv/shader_objects.py
+++ b/yt_idv/shader_objects.py
@@ -367,6 +367,7 @@ class ShaderTrait(traitlets.TraitType):
                     "blend_func_separate" in shader_info
                 )
                 shader_info.setdefault("shader_name", value)
+                shader_info.setdefault("preprocessor_defs", [("USE_RED_OFF", "")])
                 shader = Shader(**shader_info)
                 return shader
             except KeyError:
diff --git a/yt_idv/shaders/textoverlay.frag.glsl b/yt_idv/shaders/textoverlay.frag.glsl
index a6b98da..8657b5f 100644
--- a/yt_idv/shaders/textoverlay.frag.glsl
+++ b/yt_idv/shaders/textoverlay.frag.glsl
@@ -6,5 +6,9 @@ void main(){
    float val = texture(fb_tex, UV).r;
    gl_FragDepth = 0.0;
    if(val == 0) discard;
+#ifdef USE_RED
+   color = vec4(1.0, 0, 0, 1.0);
+#else
    color = vec4(val);
+#endif
 }

and switching between USE_RED_OFF and USE_RED.

matthewturk commented 1 year ago

I can't remember why I didn't just merge this!