From 8894231cd7a6098e728c67f2cd300b871cec9138 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 19 May 2017 12:32:13 -0700 Subject: [PATCH] Rewrite genshaders.sh as a Python script that uses a manifest. (bug 1365859 part 1, r=bas) --HG-- extra : rebase_source : a5b60ab1c5531c7e911fc6c6e522d4e7b8cc2497 --- gfx/layers/d3d11/DeviceAttachmentsD3D11.h | 4 +- gfx/layers/d3d11/genshaders.py | 108 ++++++++++++++++++++++ gfx/layers/d3d11/genshaders.sh | 56 ----------- gfx/layers/d3d11/shaders.manifest | 28 ++++++ gfx/vr/gfxVROculus.cpp | 6 +- 5 files changed, 143 insertions(+), 59 deletions(-) create mode 100644 gfx/layers/d3d11/genshaders.py delete mode 100644 gfx/layers/d3d11/genshaders.sh create mode 100644 gfx/layers/d3d11/shaders.manifest diff --git a/gfx/layers/d3d11/DeviceAttachmentsD3D11.h b/gfx/layers/d3d11/DeviceAttachmentsD3D11.h index 62dd35acd91a..6e2237f2e748 100644 --- a/gfx/layers/d3d11/DeviceAttachmentsD3D11.h +++ b/gfx/layers/d3d11/DeviceAttachmentsD3D11.h @@ -13,11 +13,11 @@ #include #include -struct ShaderBytes; - namespace mozilla { namespace layers { +struct ShaderBytes; + class DeviceAttachmentsD3D11 { NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DeviceAttachmentsD3D11); diff --git a/gfx/layers/d3d11/genshaders.py b/gfx/layers/d3d11/genshaders.py new file mode 100644 index 000000000000..a5eda7ebcc52 --- /dev/null +++ b/gfx/layers/d3d11/genshaders.py @@ -0,0 +1,108 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +import argparse +import codecs +import subprocess +import sys +import tempfile +import yaml + +def shell_main(): + parser = argparse.ArgumentParser() + parser.add_argument('-o', '--output', type=str, required=True, + help='Output file') + parser.add_argument('manifest', type=str, + help='Manifest source file') + args = parser.parse_args() + + with open(args.output, 'w') as out_file: + process_manifest(out_file, args.manifest) + +def main(output_fp, input_filename): + process_manifest(output_fp, input_filename) + +HEADER = """// AUTOGENERATED - DO NOT EDIT +namespace mozilla { +namespace layers { + +struct ShaderBytes { const void* mData; size_t mLength; }; +""" +FOOTER = """ +} // namespace layers +} // namespace mozilla""" + +def process_manifest(output_fp, manifest_filename): + with codecs.open(manifest_filename, 'r', 'UTF-8') as in_fp: + manifest = yaml.load(in_fp) + shader_folder, _ = os.path.split(manifest_filename) + + output_fp.write(HEADER) + + for block in manifest: + if 'type' not in block: + raise Exception("Expected 'type' key with shader mode") + if 'file' not in block: + raise Exception("Expected 'file' key with shader file") + if 'shaders' not in block: + raise Exception("Expected 'shaders' key with shader name list") + + shader_model = block['type'] + shader_file = os.path.join(shader_folder, block['file']) + for shader_name in block['shaders']: + run_fxc( + shader_model = shader_model, + shader_file = shader_file, + shader_name = shader_name, + output_fp = output_fp) + + output_fp.write(FOOTER) + +def run_fxc(shader_model, + shader_file, + shader_name, + output_fp): + argv = [ + 'fxc', + '-nologo', + '-T{0}'.format(shader_model), + shader_file, + '-E{0}'.format(shader_name), + '-Vn{0}'.format(shader_name), + ] + if shader_model.startswith('vs_'): + argv += ['-DVERTEX_SHADER'] + elif shader_model.startswith('ps_'): + argv += ['-DPIXEL_SHADER'] + + with ScopedTempFilename() as temp_filename: + argv += ['-Fh{0}'.format(temp_filename)] + + sys.stdout.write('{0}\n'.format(' '.join(argv))) + subprocess.check_call(argv) + + with open(temp_filename, 'r') as temp_fp: + output_fp.write(temp_fp.read()) + + output_fp.write("ShaderBytes s{0} = {{ {0}, sizeof({0}) }};\n".format( + shader_name)) + +# Allocate a temporary file name and delete it when done. We need an extra +# wrapper for this since TemporaryNamedFile holds the file open. +class ScopedTempFilename(object): + def __init__(self): + self.name = None + def __enter__(self): + with tempfile.NamedTemporaryFile(delete = False) as tmp: + self.name = tmp.name + return self.name + def __exit__(self, type, value, traceback): + if not self.name: + return + try: + os.unlink(self.name) + except: + pass + +if __name__ == '__main__': + shell_main() diff --git a/gfx/layers/d3d11/genshaders.sh b/gfx/layers/d3d11/genshaders.sh deleted file mode 100644 index ad477eee4b59..000000000000 --- a/gfx/layers/d3d11/genshaders.sh +++ /dev/null @@ -1,56 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -tempfile=tmpShaderHeader - -FXC_DEBUG_FLAGS="-Zi -Fd shaders.pdb" -FXC_FLAGS="" - -# If DEBUG is in the environment, then rebuild with debug info -if [ "$DEBUG" != "" ] ; then - FXC_FLAGS="$FXC_DEBUG_FLAGS" -fi - -makeShaderVS() { - fxc -nologo $FXC_FLAGS -Tvs_4_0_level_9_3 $SRC -E$1 -Vn$1 -Fh$tempfile - echo "ShaderBytes s$1 = { $1, sizeof($1) };" >> $tempfile; - cat $tempfile >> $DEST -} - -makeShaderPS() { - fxc -nologo $FXC_FLAGS -Tps_4_0_level_9_3 $SRC -E$1 -Vn$1 -Fh$tempfile - echo "ShaderBytes s$1 = { $1, sizeof($1) };" >> $tempfile; - cat $tempfile >> $DEST -} - -SRC=CompositorD3D11.hlsl -DEST=CompositorD3D11Shaders.h - -rm -f $DEST -echo "struct ShaderBytes { const void* mData; size_t mLength; };" >> $DEST; -makeShaderVS LayerQuadVS -makeShaderVS LayerDynamicVS -makeShaderPS SolidColorShader -makeShaderPS RGBShader -makeShaderPS RGBAShader -makeShaderPS ComponentAlphaShader -makeShaderPS YCbCrShader -makeShaderPS NV12Shader -makeShaderVS LayerQuadMaskVS -makeShaderVS LayerDynamicMaskVS -makeShaderPS SolidColorShaderMask -makeShaderPS RGBShaderMask -makeShaderPS RGBAShaderMask -makeShaderPS YCbCrShaderMask -makeShaderPS NV12ShaderMask -makeShaderPS ComponentAlphaShaderMask - -# Mix-blend shaders -makeShaderVS LayerQuadBlendVS -makeShaderVS LayerQuadBlendMaskVS -makeShaderVS LayerDynamicBlendVS -makeShaderVS LayerDynamicBlendMaskVS -makeShaderPS BlendShader - -rm $tempfile diff --git a/gfx/layers/d3d11/shaders.manifest b/gfx/layers/d3d11/shaders.manifest new file mode 100644 index 000000000000..f1f43dd23a2b --- /dev/null +++ b/gfx/layers/d3d11/shaders.manifest @@ -0,0 +1,28 @@ +- type: vs_4_0_level_9_3 + file: CompositorD3D11.hlsl + shaders: + - LayerQuadVS + - LayerDynamicVS + - LayerQuadMaskVS + - LayerDynamicMaskVS + - LayerQuadBlendVS + - LayerQuadBlendMaskVS + - LayerDynamicBlendVS + - LayerDynamicBlendMaskVS + +- type: ps_4_0_level_9_3 + file: CompositorD3D11.hlsl + shaders: + - SolidColorShader + - RGBShader + - RGBAShader + - ComponentAlphaShader + - YCbCrShader + - NV12Shader + - SolidColorShaderMask + - RGBShaderMask + - RGBAShaderMask + - YCbCrShaderMask + - NV12ShaderMask + - ComponentAlphaShaderMask + - BlendShader diff --git a/gfx/vr/gfxVROculus.cpp b/gfx/vr/gfxVROculus.cpp index 960b76d5ce79..fa25ede0a10c 100644 --- a/gfx/vr/gfxVROculus.cpp +++ b/gfx/vr/gfxVROculus.cpp @@ -42,9 +42,13 @@ */ // See CompositorD3D11Shaders.h +namespace mozilla { +namespace layers { struct ShaderBytes { const void* mData; size_t mLength; }; extern ShaderBytes sRGBShader; extern ShaderBytes sLayerQuadVS; +} // namespace layers +} // namespace mozilla #ifndef M_PI # define M_PI 3.14159265358979323846 #endif @@ -1504,4 +1508,4 @@ VRSystemManagerOculus::RemoveControllers() mOculusController.Clear(); mControllerCount = 0; -} \ No newline at end of file +}