rk_island/game/shader.py

59 lines
1.9 KiB
Python
Raw Normal View History

# Copyright (C) 2022 RozK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pathlib import Path
from engine import load_shader
#TODO: preprocessing (at least includes)
def _cleanup(line):
return line.partition('//')[0].strip()
def _filter(line):
return line
def _subst(line):
if line.startswith('#include '):
path = Path('.') / 'game' / 'shaders'
lines = []
for name in line.split()[1:]:
lines.extend(_load_source(path / name.strip('"')))
return lines
return [line]
def _load_source(_path):
assert _path.exists()
lines = filter(_filter, map(_cleanup, open(_path, 'rt')))
source = []
for line in lines:
source.extend(_subst(line))
return source
def _convert(line):
return bytes(line, 'utf-8') + b'\n'
def load(vert_name, frag_name = ''):
path = Path('.') / 'game' / 'shaders'
if not frag_name:
frag_name = vert_name
print("Loading vertex shader", vert_name)
vert_lines = list(map(_convert, _load_source(path / (vert_name + '_opengles.vert'))))
assert vert_lines
print("Loading fragment shader", frag_name)
frag_lines = list(map(_convert, _load_source(path / (frag_name + '_opengles.frag'))))
assert frag_lines
return load_shader(vert_lines, frag_lines)