Skip to content

Commit

Permalink
Clean up shader loaders a bit
Browse files Browse the repository at this point in the history
Preparing for new features
  • Loading branch information
einarf committed Mar 23, 2020
1 parent a82e510 commit b35f62e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
16 changes: 8 additions & 8 deletions moderngl_window/loaders/program/separate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def load(self) -> Union[
"""
prog = None

vs_source = self.load_shader("vertex", self.meta.vertex_shader)
geo_source = self.load_shader("geometry", self.meta.geometry_shader)
fs_source = self.load_shader("fragment", self.meta.fragment_shader)
tc_source = self.load_shader("tess_control", self.meta.tess_control_shader)
te_source = self.load_shader("tess_evaluation", self.meta.tess_evaluation_shader)
cs_source = self.load_shader("compute", self.meta.compute_shader)
vs_source = self._load_source("vertex", self.meta.vertex_shader)
geo_source = self._load_source("geometry", self.meta.geometry_shader)
fs_source = self._load_source("fragment", self.meta.fragment_shader)
tc_source = self._load_source("tess_control", self.meta.tess_control_shader)
te_source = self._load_source("tess_evaluation", self.meta.tess_evaluation_shader)
cs_source = self._load_source("compute", self.meta.compute_shader)

if vs_source:
shaders = program.ProgramShaders.from_separate(
Expand All @@ -58,8 +58,8 @@ def load(self) -> Union[

return prog

def load_shader(self, shader_type: str, path: str):
"""Load a single shader"""
def _load_source(self, shader_type: str, path: str):
"""Load a single shader source"""
if path:
resolved_path = self.find_program(path)
if not resolved_path:
Expand Down
28 changes: 19 additions & 9 deletions moderngl_window/loaders/program/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,8 @@ def load(self) -> moderngl.Program:
Returns:
moderngl.Program: The Program instance
"""
self.meta.resolved_path = self.find_program(self.meta.path)
if not self.meta.resolved_path:
raise ImproperlyConfigured("Cannot find program '{}'".format(self.meta.path))

logger.info("Loading: %s", self.meta.path)

with open(str(self.meta.resolved_path), 'r') as fd:
shaders = program.ProgramShaders.from_single(self.meta, fd.read())

self.meta.resolved_path, source = self._load_source(self.meta.path)
shaders = program.ProgramShaders.from_single(self.meta, source)
prog = shaders.create()

# Wrap the program if reloadable is set
Expand All @@ -72,3 +65,20 @@ def load(self) -> moderngl.Program:
prog = program.ReloadableProgram(self.meta, prog)

return prog

def _load_source(self, path):
"""Finds and loads a single source file.
Args:
path: Path to resource
Returns:
Tuple[resolved_path, source]: The resolved path and the source
"""
resolved_path = self.find_program(path)
if not resolved_path:
raise ImproperlyConfigured("Cannot find program '{}'".format(path))

logger.info("Loading: %s", path)

with open(str(resolved_path), 'r') as fd:
return resolved_path, fd.read()

0 comments on commit b35f62e

Please sign in to comment.