
# @begin header
#
# Text inside @begin/@end blocks is autogenerated by scripts/upkeep/update-glsl-sources.py.
# If this file starts with an @overwrite directive, then its entire contents are managed by the script.
#
# @end header

glsl_files = [
# @begin glsl
    'alpha_depth.frag.glsl',
    'alpha_discard.frag.glsl',
    'baryon_feedback.frag.glsl',
    'bloom.frag.glsl',
    'blur13.frag.glsl',
    'blur25.frag.glsl',
    'blur5.frag.glsl',
    'blur9.frag.glsl',
    'boss_death.frag.glsl',
    'boss_zoom.frag.glsl',
    'calabi-yau-quintic.frag.glsl',
    'calabi-yau-quintic.vert.glsl',
    'circle_distort.frag.glsl',
    'cutscene.frag.glsl',
    'envmap_reflect.frag.glsl',
    'extra_bg.frag.glsl',
    'extra_tower_apply_mask.frag.glsl',
    'extra_tower_mask.frag.glsl',
    'extra_tower_mask.vert.glsl',
    'fireparticles.frag.glsl',
    'fireparticles.vert.glsl',
    'fxaa.frag.glsl',
    'fxaa.vert.glsl',
    'glitch.frag.glsl',
    'graph.frag.glsl',
    'healthbar.vert.glsl',
    'healthbar_linear.frag.glsl',
    'healthbar_radial.frag.glsl',
    'ingame_menu.frag.glsl',
    'mainmenubg.frag.glsl',
    'marisa_laser.frag.glsl',
    'maristar_bombbg.frag.glsl',
    'masterspark.frag.glsl',
    'max_to_alpha.frag.glsl',
    'pbr.frag.glsl',
    'pbr.vert.glsl',
    'pbr_diffuse_alpha_discard.frag.glsl',
    'pbr_roughness_alpha_discard.frag.glsl',
    'pbr_water.frag.glsl',
    'pbr_water.vert.glsl',
    'player_death.frag.glsl',
    'powersurge_effect.frag.glsl',
    'powersurge_feedback.frag.glsl',
    'reimu_bomb_bg.frag.glsl',
    'reimu_gap.frag.glsl',
    'reimu_gap.vert.glsl',
    'reimu_gap_light.frag.glsl',
    'spellcard_intro.frag.glsl',
    'spellcard_outro.frag.glsl',
    'spellcard_walloftext.frag.glsl',
    'sprite_bullet.frag.glsl',
    'sprite_bullet.vert.glsl',
    'sprite_circleclipped_indicator.frag.glsl',
    'sprite_circleclipped_indicator.vert.glsl',
    'sprite_default.frag.glsl',
    'sprite_default.vert.glsl',
    'sprite_fairy.frag.glsl',
    'sprite_fairy.vert.glsl',
    'sprite_filled_circle.frag.glsl',
    'sprite_filled_circle.vert.glsl',
    'sprite_hakkero.frag.glsl',
    'sprite_hakkero.vert.glsl',
    'sprite_negative.frag.glsl',
    'sprite_particle.frag.glsl',
    'sprite_pbr.frag.glsl',
    'sprite_pbr.vert.glsl',
    'sprite_silhouette.frag.glsl',
    'sprite_silhouette.vert.glsl',
    'sprite_yinyang.frag.glsl',
    'sprite_youmu_myon_shot.frag.glsl',
    'ssr.vert.glsl',
    'ssr_water.frag.glsl',
    'stage1_water.frag.glsl',
    'stage3_wriggle_bg.frag.glsl',
    'stage6_sky.frag.glsl',
    'stage6_sky.vert.glsl',
    'standard.frag.glsl',
    'standard.vert.glsl',
    'standardnotex.frag.glsl',
    'standardnotex.vert.glsl',
    'text_cutscene.frag.glsl',
    'text_default.frag.glsl',
    'text_default.vert.glsl',
    'text_demo.frag.glsl',
    'text_demo.vert.glsl',
    'text_dialog.frag.glsl',
    'text_dialog.vert.glsl',
    'text_example.frag.glsl',
    'text_example.vert.glsl',
    'text_hud.frag.glsl',
    'text_stagetext.frag.glsl',
    'texture_post_load.frag.glsl',
    'tonemap.frag.glsl',
    'tower_wall.frag.glsl',
    'tower_wall.vert.glsl',
    'tunnel.frag.glsl',
    'youmu_bomb_bg.frag.glsl',
    'youmua_bomb.frag.glsl',
    'zbuf_fog.frag.glsl',
    'zbuf_fog_tonemap.frag.glsl',
# @end glsl
]

subdirs = [
# @begin subdirs
    'lasers',
# @end subdirs
]

spirv_targets = []
essl_targets = []

glslc_args = [
    # '-fauto-bind-uniforms',
    # '-fauto-map-locations',
    '--target-env=opengl',
    '-DSPIRV',
]

glslc_depfile_args = [
    '-MD',
    '-MF', '@DEPFILE@',
]

glslc_frag_args = [
    '-fshader-stage=frag',
    '-DFRAG_STAGE',
]

glslc_vert_args = [
    '-fshader-stage=vert',
    '-DVERT_STAGE',
]

spvc_args = [
    '--version', '300',
    '--es',
    '--remove-unused-variables',
]

spvc_frag_args = [
    '--stage', 'frag',
]

spvc_vert_args = [
    '--stage', 'vert',
]

transpile_glsl = (host_machine.system() in ['emscripten', 'nx'])
opt_validate_glsl = get_option('validate_glsl')

if opt_validate_glsl.disabled() and transpile_glsl
    error('GLSL validation can\'t be disabled on this platform')
endif

force_vendored_shader_tools = get_option('force_vendored_shader_tools')
force_validate_glsl = (
    opt_validate_glsl.enabled() or
    transpile_glsl or
    (force_vendored_shader_tools and opt_validate_glsl.allowed())
)

validate_glsl = false

if opt_validate_glsl.allowed()
    if force_vendored_shader_tools
        glslc_command = disabler()
    else
        glslc_command = find_program('glslc', required : false)
    endif

    if not glslc_command.found() and force_validate_glsl
        glslc_command = subproject('shaderc').get_variable('glslc_native')
        glslc_args += '-Werror'
        validate_glsl = true
    elif glslc_command.found()
        test_result = run_command(glslc_command,
            glslc_args,
            glslc_frag_args,
            glslc_test_uniform_location_file,
            '-o', '-',
            capture : false,
            check : false
        )

        if test_result.returncode() == 0
            glslc_args += '-Werror'
        else
            glslc_args += '-std=430core'
            warning('glslc does not support GL_ARB_explicit_uniform_location, forcing shader version to 430core as a workaround.\n\n@0@'.format(test_result.stderr()))
        endif

        test_result = run_command(glslc_command,
            glslc_args,
            glslc_frag_args,
            glslc_test_basic_file,
            '-o', '-',
            capture : false,
            check : false
        )

        if test_result.returncode() == 0
            validate_glsl = true
        else
            warning(test_result.stderr())

            if force_validate_glsl
                error('Basic glslc test failed, you probably have a broken/incompatible version.')
            endif

            warning('Basic glslc test failed, you probably have a broken/incompatible version. GLSL validation will be disabled.')
            validate_glsl = false
        endif
    else
        warning('GLSL validation or translation requested, but glslc was not found')
        validate_glsl = false
    endif

    if validate_glsl and transpile_glsl
        if force_vendored_shader_tools
            spvc_command = disabler()
        else
            spvc_command = find_program('spirv-cross', required : false)
        endif

        if not spvc_command.found()
            spvc_command = subproject('SPIRV-Cross').get_variable('spirv_cross_native')
        endif

        glslc_args += ['-Os', '-g']
    endif
endif

transpile_glsl = transpile_glsl and validate_glsl

# @begin validate
if validate_glsl
    foreach fname : glsl_files
        message(f'fname: @fname@')
        stage = fname.split('.')[-2]

        spirv = custom_target(
            input : fname,
            output : '@BASENAME@.spv',
            command : [
                glslc_command, glslc_args, glslc_depfile_args, get_variable(f'glslc_@stage@_args'),
                '@INPUT@', '-o', '@OUTPUT@'
            ],
            install : false,
            build_by_default : true,
            depfile : f'@fname@.d',
        )

        spirv_targets += spirv

        if transpile_glsl
            essl = custom_target(
                input : spirv,
                output : '@BASENAME@.glsl',
                command : [
                    spvc_command,
                    '--output', '@OUTPUT@', '@INPUT@',
                    spvc_args,
                    get_variable(f'spvc_@stage@_args'),
                ],
                install : false,
                build_by_default : true,
                depfile : f'@fname@.d',
            )

            essl_targets += essl
        endif
    endforeach
endif
# @end validate

foreach sd : subdirs
    subdir(sd)
endforeach

shaders_build_dir = meson.current_build_dir()
