Rewrite genshaders.sh as a Python script that uses a manifest. (bug 1365859 part 1, r=bas)
authorDavid Anderson <danderson@mozilla.com>
Fri, 19 May 2017 12:32:13 -0700
changeset 359286 2323c4c38c31ee1bcdd7bf07b7878a8ecf82bef2
parent 359285 4e40b5b294428fc51707a47a471cdb5d90424fdd
child 359287 5911d850652a701e5ef0746327ca52dee5123200
push id90487
push userdanderson@mozilla.com
push dateFri, 19 May 2017 19:42:26 +0000
treeherdermozilla-inbound@5911d850652a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbas
bugs1365859
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Rewrite genshaders.sh as a Python script that uses a manifest. (bug 1365859 part 1, r=bas)
gfx/layers/d3d11/DeviceAttachmentsD3D11.h
gfx/layers/d3d11/genshaders.py
gfx/layers/d3d11/genshaders.sh
gfx/layers/d3d11/shaders.manifest
gfx/vr/gfxVROculus.cpp
--- a/gfx/layers/d3d11/DeviceAttachmentsD3D11.h
+++ b/gfx/layers/d3d11/DeviceAttachmentsD3D11.h
@@ -8,21 +8,21 @@
 
 #include "mozilla/EnumeratedArray.h"
 #include "mozilla/RefPtr.h"
 #include "mozilla/gfx/DeviceManagerDx.h"
 #include "mozilla/layers/CompositorTypes.h"
 #include <d3d11.h>
 #include <dxgi1_2.h>
 
-struct ShaderBytes;
-
 namespace mozilla {
 namespace layers {
 
+struct ShaderBytes;
+
 class DeviceAttachmentsD3D11
 {
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DeviceAttachmentsD3D11);
 
 public:
   static RefPtr<DeviceAttachmentsD3D11> Create(ID3D11Device* aDevice);
 
   bool InitBlendShaders();
new file mode 100644
--- /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()
deleted file mode 100644
--- 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
new file mode 100644
--- /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
--- a/gfx/vr/gfxVROculus.cpp
+++ b/gfx/vr/gfxVROculus.cpp
@@ -37,19 +37,23 @@
  *    into a separate object if either Oculus starts supporting
  *     non-Windows platforms or the blit is needed by other HMD\
  *     drivers.
  *     Alternately, we could remove the extra blit for
  *     Oculus as well with some more refactoring.
  */
 
 // 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
 
 using namespace mozilla;
 using namespace mozilla::gfx;
 using namespace mozilla::gfx::impl;
 using namespace mozilla::layers;
@@ -1499,9 +1503,9 @@ VRSystemManagerOculus::RemoveControllers
 {
   // controller count is changed, removing the existing gamepads first.
   for (uint32_t i = 0; i < mOculusController.Length(); ++i) {
     RemoveGamepad(i);
   }
 
   mOculusController.Clear();
   mControllerCount = 0;
-}
\ No newline at end of file
+}