2022-12-04 03:23:03 +01:00
|
|
|
# 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)
|
|
|
|
|
2022-12-31 14:38:53 +01:00
|
|
|
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'
|
|
|
|
|
2022-12-18 04:53:28 +01:00
|
|
|
def load(vert_name, frag_name = ''):
|
|
|
|
path = Path('.') / 'game' / 'shaders'
|
|
|
|
if not frag_name:
|
|
|
|
frag_name = vert_name
|
|
|
|
print("Loading vertex shader", vert_name)
|
2022-12-31 14:38:53 +01:00
|
|
|
vert_lines = list(map(_convert, _load_source(path / (vert_name + '_opengles.vert'))))
|
|
|
|
assert vert_lines
|
2022-12-18 04:53:28 +01:00
|
|
|
print("Loading fragment shader", frag_name)
|
2022-12-31 14:38:53 +01:00
|
|
|
frag_lines = list(map(_convert, _load_source(path / (frag_name + '_opengles.frag'))))
|
|
|
|
assert frag_lines
|
2022-12-04 03:23:03 +01:00
|
|
|
return load_shader(vert_lines, frag_lines)
|