Commit 5b7c5b34 by Le Hoang Quyen Committed by Commit Bot

Metal: don't precompile default shaders.

Default shaders will be compiled from source at runtime, this is because they depend on ANGLE format table and there is currently no way to pre-compile metal shaders in a cross-platform manner. Using default shaders' source instead of pre-compiled form seems to reduce the libGLESv2's binary size. However, the startup time will be increased due to runtime cost of compilation, thus the compilation now will be done asynchronously. Bug: angleproject:5186 Change-Id: I0e1987d6c76692d5169255736fbb8e215185c33b Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2482405Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Commit-Queue: Le Hoang Quyen <le.hoang.q@gmail.com>
parent 9047954c
...@@ -6,49 +6,25 @@ ...@@ -6,49 +6,25 @@
"src/libANGLE/renderer/gen_angle_format_table.py": "src/libANGLE/renderer/gen_angle_format_table.py":
"34ab57bb88958e320f509bd5fbd04495", "34ab57bb88958e320f509bd5fbd04495",
"src/libANGLE/renderer/metal/shaders/blit.metal": "src/libANGLE/renderer/metal/shaders/blit.metal":
"502660301ee21e5cd4a5dd42a8a24e82", "2f6286729703039d41d0f6a109a23ac2",
"src/libANGLE/renderer/metal/shaders/clear.metal": "src/libANGLE/renderer/metal/shaders/clear.metal":
"37ef05208eb5d12110a7d277a4bbcbe1", "37ef05208eb5d12110a7d277a4bbcbe1",
"src/libANGLE/renderer/metal/shaders/common.h": "src/libANGLE/renderer/metal/shaders/common.h":
"0cadef47fb785aa3102acb2a6eae94bb", "ac1bdd21a1cee65d88cfc4e0e61894da",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_2_1_debug_ios_autogen.inc":
"2dfc9dc2496c93fe6c4d46661d77a14b",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_2_1_debug_ios_sim_autogen.inc":
"e4d6ce5e3081e18908b8c95f7ed372b2",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_2_1_debug_mac_autogen.inc":
"79cb485ee377adac2632adaa21e2c8ac",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_2_1_ios_autogen.inc":
"55abd74424bdd88b5523534ec867f715",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_2_1_ios_sim_autogen.inc":
"1cf20c8cb47ed5990c8c165b7faa1c0d",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_2_1_mac_autogen.inc":
"a4b9067e64c6d0dcfcff3422f833779c",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_debug_ios_autogen.inc":
"af51783bbc7dc7d6ad17f88cc5409222",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_debug_ios_sim_autogen.inc":
"25e0ecdaf8829af2ebed1738bf57b2d9",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_debug_mac_autogen.inc":
"34885f0482b73513f1736eb8be340d66",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_ios_autogen.inc":
"f22b1f09a53bc5d1f9a462f4552aa116",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_ios_sim_autogen.inc":
"7de301390d329a1493a557c60ac9888c",
"src/libANGLE/renderer/metal/shaders/compiled/compiled_default_metallib_mac_autogen.inc":
"cd1dbbd9b961aaef467cc24c5c1abcb0",
"src/libANGLE/renderer/metal/shaders/compiled/mtl_default_shaders_autogen.inc":
"634a127f4e94f6bc3123e89850d010ee",
"src/libANGLE/renderer/metal/shaders/constants.h": "src/libANGLE/renderer/metal/shaders/constants.h":
"dad1a869a1095be669b7da5651901d38", "dad1a869a1095be669b7da5651901d38",
"src/libANGLE/renderer/metal/shaders/copy_buffer.metal": "src/libANGLE/renderer/metal/shaders/copy_buffer.metal":
"97b258edbade1ed088e4c03a1102f974", "813e16a38d6e3ba858b62a712b1b316d",
"src/libANGLE/renderer/metal/shaders/format_autogen.h": "src/libANGLE/renderer/metal/shaders/format_autogen.h":
"b1d6512b904a7eb151b0095b7898b0e5", "b1d6512b904a7eb151b0095b7898b0e5",
"src/libANGLE/renderer/metal/shaders/gen_indices.metal": "src/libANGLE/renderer/metal/shaders/gen_indices.metal":
"87a76d5e12825111c0595f69e79f5d20", "add45aa44305b1a64c4bb8ece1e3d2fc",
"src/libANGLE/renderer/metal/shaders/gen_mipmap.metal": "src/libANGLE/renderer/metal/shaders/gen_mipmap.metal":
"54dca94c48bead446624079070b9b309", "54dca94c48bead446624079070b9b309",
"src/libANGLE/renderer/metal/shaders/gen_mtl_internal_shaders.py": "src/libANGLE/renderer/metal/shaders/gen_mtl_internal_shaders.py":
"d5f23d645c0cb9d487e40d490414a772", "9f538745533b6bb14fbbc9e4252f31e0",
"src/libANGLE/renderer/metal/shaders/mtl_default_shaders_src_autogen.inc":
"2a0b015ebec36ee1304ae64a7187abb8",
"src/libANGLE/renderer/metal/shaders/visibility.metal": "src/libANGLE/renderer/metal/shaders/visibility.metal":
"998d705656c63849edd0187cd8062fc7" "b82aa740cf4b0aed606aacef1024beea"
} }
\ No newline at end of file
...@@ -62,21 +62,9 @@ _metal_backend_sources = [ ...@@ -62,21 +62,9 @@ _metal_backend_sources = [
"mtl_state_cache.mm", "mtl_state_cache.mm",
"mtl_utils.h", "mtl_utils.h",
"mtl_utils.mm", "mtl_utils.mm",
"shaders/compiled/compiled_default_metallib_2_1_debug_ios_autogen.inc",
"shaders/compiled/compiled_default_metallib_2_1_debug_ios_sim_autogen.inc",
"shaders/compiled/compiled_default_metallib_2_1_debug_mac_autogen.inc",
"shaders/compiled/compiled_default_metallib_2_1_ios_autogen.inc",
"shaders/compiled/compiled_default_metallib_2_1_ios_sim_autogen.inc",
"shaders/compiled/compiled_default_metallib_2_1_mac_autogen.inc",
"shaders/compiled/compiled_default_metallib_debug_ios_autogen.inc",
"shaders/compiled/compiled_default_metallib_debug_ios_sim_autogen.inc",
"shaders/compiled/compiled_default_metallib_debug_mac_autogen.inc",
"shaders/compiled/compiled_default_metallib_ios_autogen.inc",
"shaders/compiled/compiled_default_metallib_ios_sim_autogen.inc",
"shaders/compiled/compiled_default_metallib_mac_autogen.inc",
"shaders/compiled/mtl_default_shaders_autogen.inc",
"shaders/constants.h", "shaders/constants.h",
"shaders/format_autogen.h", "shaders/format_autogen.h",
"shaders/mtl_default_shaders_src_autogen.inc",
] ]
config("angle_metal_backend_config") { config("angle_metal_backend_config") {
......
...@@ -32,6 +32,8 @@ class ShareGroupMtl : public ShareGroupImpl ...@@ -32,6 +32,8 @@ class ShareGroupMtl : public ShareGroupImpl
class ContextMtl; class ContextMtl;
struct DefaultShaderAsyncInfoMtl;
class DisplayMtl : public DisplayImpl class DisplayMtl : public DisplayImpl
{ {
public: public:
...@@ -123,7 +125,7 @@ class DisplayMtl : public DisplayImpl ...@@ -123,7 +125,7 @@ class DisplayMtl : public DisplayImpl
mtl::RenderUtils &getUtils() { return mUtils; } mtl::RenderUtils &getUtils() { return mUtils; }
mtl::StateCache &getStateCache() { return mStateCache; } mtl::StateCache &getStateCache() { return mStateCache; }
id<MTLLibrary> getDefaultShadersLib() const { return mDefaultShaders; } id<MTLLibrary> getDefaultShadersLib();
id<MTLDepthStencilState> getDepthStencilState(const mtl::DepthStencilDesc &desc) id<MTLDepthStencilState> getDepthStencilState(const mtl::DepthStencilDesc &desc)
{ {
...@@ -175,7 +177,7 @@ class DisplayMtl : public DisplayImpl ...@@ -175,7 +177,7 @@ class DisplayMtl : public DisplayImpl
mtl::RenderUtils mUtils; mtl::RenderUtils mUtils;
// Built-in Shaders // Built-in Shaders
mtl::AutoObjCPtr<id<MTLLibrary>> mDefaultShaders = nil; std::shared_ptr<DefaultShaderAsyncInfoMtl> mDefaultShadersAsyncInfo;
#if ANGLE_MTL_EVENT_AVAILABLE #if ANGLE_MTL_EVENT_AVAILABLE
mtl::AutoObjCObj<MTLSharedEventListener> mSharedEventListener; mtl::AutoObjCObj<MTLSharedEventListener> mSharedEventListener;
#endif #endif
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include "libANGLE/renderer/metal/SurfaceMtl.h" #include "libANGLE/renderer/metal/SurfaceMtl.h"
#include "libANGLE/renderer/metal/SyncMtl.h" #include "libANGLE/renderer/metal/SyncMtl.h"
#include "libANGLE/renderer/metal/mtl_common.h" #include "libANGLE/renderer/metal/mtl_common.h"
#include "libANGLE/renderer/metal/shaders/compiled/mtl_default_shaders_autogen.inc" #include "libANGLE/renderer/metal/shaders/mtl_default_shaders_src_autogen.inc"
#include "platform/Platform.h" #include "platform/Platform.h"
#include "EGL/eglext.h" #include "EGL/eglext.h"
...@@ -44,6 +44,18 @@ DisplayImpl *CreateMetalDisplay(const egl::DisplayState &state) ...@@ -44,6 +44,18 @@ DisplayImpl *CreateMetalDisplay(const egl::DisplayState &state)
return new DisplayMtl(state); return new DisplayMtl(state);
} }
struct DefaultShaderAsyncInfoMtl
{
mtl::AutoObjCPtr<id<MTLLibrary>> defaultShaders;
mtl::AutoObjCPtr<NSError *> defaultShadersCompileError;
// Synchronization primitives for compiling default shaders in back-ground
std::condition_variable cv;
std::mutex lock;
bool compiled = false;
};
DisplayMtl::DisplayMtl(const egl::DisplayState &state) DisplayMtl::DisplayMtl(const egl::DisplayState &state)
: DisplayImpl(state), mUtils(this), mGlslangInitialized(false) : DisplayImpl(state), mUtils(this), mGlslangInitialized(false)
{} {}
...@@ -100,8 +112,8 @@ void DisplayMtl::terminate() ...@@ -100,8 +112,8 @@ void DisplayMtl::terminate()
{ {
mUtils.onDestroy(); mUtils.onDestroy();
mCmdQueue.reset(); mCmdQueue.reset();
mDefaultShaders = nil; mDefaultShadersAsyncInfo = nullptr;
mMetalDevice = nil; mMetalDevice = nil;
#if ANGLE_MTL_EVENT_AVAILABLE #if ANGLE_MTL_EVENT_AVAILABLE
mSharedEventListener = nil; mSharedEventListener = nil;
#endif #endif
...@@ -751,48 +763,62 @@ void DisplayMtl::initializeFeatures() ...@@ -751,48 +763,62 @@ void DisplayMtl::initializeFeatures()
angle::Result DisplayMtl::initializeShaderLibrary() angle::Result DisplayMtl::initializeShaderLibrary()
{ {
mtl::AutoObjCObj<NSError> err = nil; mDefaultShadersAsyncInfo.reset(new DefaultShaderAsyncInfoMtl);
const uint8_t *compiled_shader_binary; // Create references to async info struct since it might be released in terminate(), but the
size_t compiled_shader_binary_len; // callback might still not be fired yet.
std::shared_ptr<DefaultShaderAsyncInfoMtl> asyncRef = mDefaultShadersAsyncInfo;
#if !defined(NDEBUG) // Compile the default shaders asynchronously
if (getFeatures().hasStencilOutput.enabled) ANGLE_MTL_OBJC_SCOPE
{
compiled_shader_binary = compiled_default_metallib_2_1_debug;
compiled_shader_binary_len = compiled_default_metallib_2_1_debug_len;
}
else
{
compiled_shader_binary = compiled_default_metallib_debug;
compiled_shader_binary_len = compiled_default_metallib_debug_len;
}
#else
if (getFeatures().hasStencilOutput.enabled)
{ {
compiled_shader_binary = compiled_default_metallib_2_1; auto nsSource = [[NSString alloc] initWithBytesNoCopy:gDefaultMetallibSrc
compiled_shader_binary_len = compiled_default_metallib_2_1_len; length:sizeof(gDefaultMetallibSrc)
encoding:NSUTF8StringEncoding
freeWhenDone:NO];
auto options = [[[MTLCompileOptions alloc] init] ANGLE_MTL_AUTORELEASE];
[getMetalDevice() newLibraryWithSource:nsSource
options:options
completionHandler:^(id<MTLLibrary> library, NSError *error) {
std::unique_lock<std::mutex> lg(asyncRef->lock);
asyncRef->defaultShaders = std::move(library);
asyncRef->defaultShadersCompileError = std::move(error);
asyncRef->compiled = true;
asyncRef->cv.notify_one();
}];
[nsSource ANGLE_MTL_AUTORELEASE];
} }
else
return angle::Result::Continue;
}
id<MTLLibrary> DisplayMtl::getDefaultShadersLib()
{
std::unique_lock<std::mutex> lg(mDefaultShadersAsyncInfo->lock);
if (!mDefaultShadersAsyncInfo->compiled)
{ {
compiled_shader_binary = compiled_default_metallib; // Wait for async compilation
compiled_shader_binary_len = compiled_default_metallib_len; mDefaultShadersAsyncInfo->cv.wait(lg,
[this] { return mDefaultShadersAsyncInfo->compiled; });
} }
#endif
mDefaultShaders = CreateShaderLibraryFromBinary(getMetalDevice(), compiled_shader_binary, if (mDefaultShadersAsyncInfo->defaultShadersCompileError &&
compiled_shader_binary_len, &err); !mDefaultShadersAsyncInfo->defaultShaders)
if (err && !mDefaultShaders)
{ {
ANGLE_MTL_OBJC_SCOPE ANGLE_MTL_OBJC_SCOPE
{ {
ERR() << "Internal error: " << err.get().localizedDescription.UTF8String; ERR() << "Internal error: "
<< mDefaultShadersAsyncInfo->defaultShadersCompileError.get()
.localizedDescription.UTF8String;
} }
return angle::Result::Stop; // This is not supposed to happen
UNREACHABLE();
} }
return angle::Result::Continue; return mDefaultShadersAsyncInfo->defaultShaders;
} }
bool DisplayMtl::supportsIOSGPUFamily(uint8_t iOSFamily) const bool DisplayMtl::supportsIOSGPUFamily(uint8_t iOSFamily) const
......
...@@ -334,7 +334,7 @@ kernel void blitStencilToBufferCS(ushort2 gIndices [[thread_position_in_grid]], ...@@ -334,7 +334,7 @@ kernel void blitStencilToBufferCS(ushort2 gIndices [[thread_position_in_grid]],
} }
// Fragment's stencil output is only available since Metal 2.1 // Fragment's stencil output is only available since Metal 2.1
#if __METAL_VERSION__ >= 210 @@#if __METAL_VERSION__ >= 210
struct FragmentStencilOut struct FragmentStencilOut
{ {
...@@ -397,4 +397,4 @@ fragment FragmentDepthStencilOut blitDepthStencilFS( ...@@ -397,4 +397,4 @@ fragment FragmentDepthStencilOut blitDepthStencilFS(
srcStencilTextureCube, input.texCoords, options.srcLevel, options.srcLayer); srcStencilTextureCube, input.texCoords, options.srcLevel, options.srcLayer);
return re; return re;
} }
#endif // __METAL_VERSION__ >= 210 @@#endif // __METAL_VERSION__ >= 210
...@@ -8,9 +8,10 @@ ...@@ -8,9 +8,10 @@
#ifndef LIBANGLE_RENDERER_METAL_SHADERS_COMMON_H_ #ifndef LIBANGLE_RENDERER_METAL_SHADERS_COMMON_H_
#define LIBANGLE_RENDERER_METAL_SHADERS_COMMON_H_ #define LIBANGLE_RENDERER_METAL_SHADERS_COMMON_H_
// clang-format off
#ifndef SKIP_STD_HEADERS #ifndef SKIP_STD_HEADERS
# include <simd/simd.h> @@# include <simd/simd.h>
# include <metal_stdlib> @@# include <metal_stdlib>
#endif #endif
#include "constants.h" #include "constants.h"
...@@ -22,6 +23,7 @@ ...@@ -22,6 +23,7 @@
} }
using namespace metal; using namespace metal;
// clang-format on
// Common constant defined number of color outputs // Common constant defined number of color outputs
constant uint32_t kNumColorOutputs [[function_constant(0)]]; constant uint32_t kNumColorOutputs [[function_constant(0)]];
......
...@@ -9,17 +9,17 @@ ...@@ -9,17 +9,17 @@
// be a pain to implement without the use of macros. // be a pain to implement without the use of macros.
// //
#include <metal_pack> @@#include <metal_pack>
#include "common.h" #include "common.h"
#include "format_autogen.h" #include "format_autogen.h"
using namespace rx::mtl_shader; using namespace rx::mtl_shader;
constant int kCopyFormatType [[function_constant(1)]]; constant int kCopyFormatType [[function_constant(10)]];
/* -------- copy pixel data between buffer and texture ---------*/ /* -------- copy pixel data between buffer and texture ---------*/
constant int kCopyTextureType [[function_constant(2)]]; constant int kCopyTextureType [[function_constant(20)]];
constant bool kCopyTextureType2D = kCopyTextureType == kTextureType2D; constant bool kCopyTextureType2D = kCopyTextureType == kTextureType2D;
constant bool kCopyTextureType2DArray = kCopyTextureType == kTextureType2DArray; constant bool kCopyTextureType2DArray = kCopyTextureType == kTextureType2DArray;
constant bool kCopyTextureType2DMS = kCopyTextureType == kTextureType2DMultisample; constant bool kCopyTextureType2DMS = kCopyTextureType == kTextureType2DMultisample;
......
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
using namespace rx::mtl_shader; using namespace rx::mtl_shader;
// function_constant(0) is already used by common.h // function_constant(0) is already used by common.h
constant bool kSourceBufferAligned[[function_constant(1)]]; constant bool kSourceBufferAligned[[function_constant(100)]];
constant bool kSourceIndexIsU8[[function_constant(2)]]; constant bool kSourceIndexIsU8[[function_constant(200)]];
constant bool kSourceIndexIsU16[[function_constant(3)]]; constant bool kSourceIndexIsU16[[function_constant(300)]];
constant bool kSourceIndexIsU32[[function_constant(4)]]; constant bool kSourceIndexIsU32[[function_constant(400)]];
constant bool kSourceBufferUnaligned = !kSourceBufferAligned; constant bool kSourceBufferUnaligned = !kSourceBufferAligned;
constant bool kUseSourceBufferU8 = kSourceIndexIsU8 || kSourceBufferUnaligned; constant bool kUseSourceBufferU8 = kSourceIndexIsU8 || kSourceBufferUnaligned;
constant bool kUseSourceBufferU16 = kSourceIndexIsU16 && kSourceBufferAligned; constant bool kUseSourceBufferU16 = kSourceIndexIsU16 && kSourceBufferAligned;
......
...@@ -25,146 +25,6 @@ template_header_boilerplate = """// GENERATED FILE - DO NOT EDIT. ...@@ -25,146 +25,6 @@ template_header_boilerplate = """// GENERATED FILE - DO NOT EDIT.
// //
""" """
# Convert content of a file to byte array and append to a header file.
# variable_name: name of C++ variable that will hold the file content as byte array.
# filename: the file whose content will be converted to C++ byte array.
# dest_src_file: destination header file that will contain the byte array.
def append_file_as_byte_array_string(variable_name, filename, dest_src_file):
string = '// Generated from {0}:\n'.format(filename)
string += 'constexpr\nunsigned char {0}[] = {{\n'.format(variable_name)
bytes_ = open(filename, "rb").read()
byteCounter = 0
for byte in bytes_:
if byteCounter == 0:
string += " "
string += '0x{:02x}'.format(ord(byte)) + ","
byteCounter += 1
if byteCounter == 12:
byteCounter = 0
string += "\n"
else:
string += " "
string += "\n};\n"
string += 'constexpr\nsize_t {0}_len = sizeof({0});\n'.format(variable_name)
with open(dest_src_file, "a") as out_file:
out_file.write(string)
# Convert content of a file to byte array and store in a header file.
# variable_name: name of C++ variable that will hold the file content as byte array.
# copyright_comments: copyright comments
# filename: the file whose content will be converted to C++ byte array.
# dest_src_file: destination header file that will contain the byte array.
def store_file_as_byte_array_string(variable_name, copyright_comments, filename, dest_src_file):
os.system("echo \"{0}\" > {1}".format(copyright_comments, dest_src_file))
append_file_as_byte_array_string(variable_name, filename, dest_src_file)
# Convert content of a metallib to byte array and store in a header file, then include this header in
# mtl_default_shaders_autogen.inc.
# variable_name: name of C++ variable that will hold the file content as byte array.
# copyright_comments: copyright comments
# filename: the metallib file whose content will be converted to C++ byte array.
def store_metallib_as_byte_array_and_include(variable_name, copyright_comments, filename):
array_autogen_filename = "{0}_autogen.inc".format(
filename.replace('.metallib', '').replace('.', '_'))
# Generate a header containing the file's content as C array.
store_file_as_byte_array_string(variable_name, copyright_comments, filename,
array_autogen_filename)
# Include this file in mtl_default_shaders_autogen.inc
# NOTE: filename already includes "compiled/"
include_code = '#include "{0}"\n'.format(array_autogen_filename.replace('compiled/', ''))
with open('compiled/mtl_default_shaders_autogen.inc', "a") as out_file:
out_file.write(include_code)
# Compile metal shader.
# mac_version: target version of macOS
# ios_version: target version of iOS
# variable_name: name of C++ variable that will hold the compiled binary data as a C array.
# additional_flags: additional shader compiler flags
# src_files: metal source files
def gen_precompiled_shaders(mac_version, ios_version, variable_name, additional_flags, src_files,
copyright_comments):
print('Generating default shaders with flags=\'{0}\' ...'.format(additional_flags))
# Mac version's compilation
print('Compiling macos {0} version of default shaders ...'.format(mac_version))
mac_metallib = 'compiled/{0}.mac.metallib'.format(variable_name)
object_files = ''
for src_file in src_files:
object_file = 'compiled/default.{0}.{1}.air'.format(mac_version, src_file)
object_files += ' ' + object_file
os.system('xcrun -sdk macosx metal -mmacosx-version-min={0} {1} {2} -c -o {3}'.format(
mac_version, additional_flags, src_file, object_file))
os.system('xcrun -sdk macosx metallib {object_files} -o {file}'.format(
file=mac_metallib, object_files=object_files))
# iOS device version's compilation
print('Compiling ios {0} version of default shaders ...'.format(ios_version))
ios_metallib = 'compiled/{0}.ios.metallib'.format(variable_name)
object_files = ''
for src_file in src_files:
object_file = 'compiled/default.ios.{0}.{1}.air'.format(ios_version, src_file)
object_files += ' ' + object_file
os.system('xcrun -sdk iphoneos metal -mios-version-min={0} {1} {2} -c -o {3}'.format(
ios_version, additional_flags, src_file, object_file))
os.system('xcrun -sdk iphoneos metallib {object_files} -o {file}'.format(
file=ios_metallib, object_files=object_files))
# iOS simulator version's compilation
print('Compiling ios {0} simulator version of default shaders ...'.format(ios_version))
ios_sim_metallib = 'compiled/{0}.ios_sim.metallib'.format(variable_name)
object_files = ''
for src_file in src_files:
object_file = 'compiled/default.ios_sim.{0}.{1}.air'.format(ios_version, src_file)
object_files += ' ' + object_file
os.system('xcrun -sdk iphonesimulator metal {0} {1} -c -o {2}'.format(
additional_flags, src_file, object_file))
os.system('xcrun -sdk iphonesimulator metallib {object_files} -o {file}'.format(
file=ios_sim_metallib, object_files=object_files))
# Mac version's byte array string
os.system(
'echo "#if TARGET_OS_OSX || TARGET_OS_MACCATALYST\n" >> compiled/mtl_default_shaders_autogen.inc'
)
store_metallib_as_byte_array_and_include(variable_name, copyright_comments, mac_metallib)
# iOS simulator version's byte array string
os.system(
'echo "\n#elif TARGET_OS_IOS && TARGET_OS_SIMULATOR // TARGET_OS_OSX || TARGET_OS_MACCATALYST\n" >> compiled/mtl_default_shaders_autogen.inc'
)
store_metallib_as_byte_array_and_include(variable_name, copyright_comments, ios_sim_metallib)
# iOS version's byte array string
os.system(
'echo "\n#elif TARGET_OS_IOS // TARGET_OS_OSX || TARGET_OS_MACCATALYST\n" >> compiled/mtl_default_shaders_autogen.inc'
)
store_metallib_as_byte_array_and_include(variable_name, copyright_comments, ios_metallib)
os.system(
'echo "#endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST\n" >> compiled/mtl_default_shaders_autogen.inc'
)
os.system('rm -rfv compiled/*.air')
os.system('rm -rfv compiled/*.metallib')
def gen_shader_enums_code(angle_formats): def gen_shader_enums_code(angle_formats):
code = """// This file is similar to src/libANGLE/renderer/FormatID_autogen.h but is used by Metal default code = """// This file is similar to src/libANGLE/renderer/FormatID_autogen.h but is used by Metal default
...@@ -200,28 +60,10 @@ def main(): ...@@ -200,28 +60,10 @@ def main():
'visibility.metal' 'visibility.metal'
] ]
# yapf: disable
os_specific_autogen_files = [
'compiled/compiled_default_metallib_2_1_debug_ios_autogen.inc',
'compiled/compiled_default_metallib_2_1_debug_ios_sim_autogen.inc',
'compiled/compiled_default_metallib_2_1_debug_mac_autogen.inc',
'compiled/compiled_default_metallib_2_1_ios_autogen.inc',
'compiled/compiled_default_metallib_2_1_ios_sim_autogen.inc',
'compiled/compiled_default_metallib_2_1_mac_autogen.inc',
'compiled/compiled_default_metallib_debug_ios_autogen.inc',
'compiled/compiled_default_metallib_debug_ios_sim_autogen.inc',
'compiled/compiled_default_metallib_debug_mac_autogen.inc',
'compiled/compiled_default_metallib_ios_autogen.inc',
'compiled/compiled_default_metallib_ios_sim_autogen.inc',
'compiled/compiled_default_metallib_mac_autogen.inc',
]
# yapf: enable
# auto_script parameters. # auto_script parameters.
if len(sys.argv) > 1: if len(sys.argv) > 1:
inputs = angle_format_script_files + src_files + ['common.h', 'constants.h'] inputs = angle_format_script_files + src_files + ['common.h', 'constants.h']
outputs = ['format_autogen.h', 'compiled/mtl_default_shaders_autogen.inc' outputs = ['format_autogen.h', 'mtl_default_shaders_src_autogen.inc']
] + os_specific_autogen_files
if sys.argv[1] == 'inputs': if sys.argv[1] == 'inputs':
print ','.join(inputs) print ','.join(inputs)
...@@ -246,28 +88,41 @@ def main(): ...@@ -246,28 +88,41 @@ def main():
out_file.write(shader_autogen_header) out_file.write(shader_autogen_header)
out_file.close() out_file.close()
# -------- Compile shaders ----------- # -------- Combine and create shader source string -----------
# boilerplate code # Generate a combination source
os.system("echo \"{0}\" > compiled/mtl_default_shaders_autogen.inc".format(boilerplate_code)) os.system('mkdir -p temp && rm -f temp/master_source.metal && touch temp/master_source.metal')
os.system( for src_file in src_files:
'echo "// Compiled binary for Metal default shaders.\n\n" >> compiled/mtl_default_shaders_autogen.inc' os.system('echo "#include \\"../{0}\\"" >> temp/master_source.metal'.format(src_file))
)
os.system( # Use clang/gcc to preprocess the combination source. "@@" token is used to prevent clang from
'echo "#include <TargetConditionals.h>\n\n" >> compiled/mtl_default_shaders_autogen.inc') # expanding the preprocessor directive
if os.system('which gcc') == 0:
os.system('echo "// clang-format off" >> compiled/mtl_default_shaders_autogen.inc') print('combining source files using gcc -E')
os.system('gcc -xc++ -E temp/master_source.metal > temp/master_source.metal.pp')
else:
print('combining source files using clang -E')
os.system('clang -xc++ -E temp/master_source.metal > temp/master_source.metal.pp')
# Remove '@@' tokens
final_combined_src_string = ''
with open('temp/master_source.metal.pp', 'rt') as in_file:
final_combined_src_string = in_file.read().replace('@@', '')
in_file.close()
# Generate final file:
with open('mtl_default_shaders_src_autogen.inc', 'wt') as out_file:
out_file.write(boilerplate_code)
out_file.write('\n')
out_file.write('// C++ string version of combined Metal default shaders.\n\n')
out_file.write('\n\nstatic char gDefaultMetallibSrc[] = R"(\n')
out_file.write(final_combined_src_string)
out_file.write('\n')
out_file.write(')";\n')
out_file.close()
# pre-compiled shaders # Clean up
gen_precompiled_shaders(10.13, 11.0, 'compiled_default_metallib', '', src_files, os.system('rm -rf temp')
boilerplate_code)
gen_precompiled_shaders(10.13, 11.0, 'compiled_default_metallib_debug',
'-gline-tables-only -MO', src_files, boilerplate_code)
gen_precompiled_shaders(10.14, 12.0, 'compiled_default_metallib_2_1', '', src_files,
boilerplate_code)
gen_precompiled_shaders(10.14, 12.0, 'compiled_default_metallib_2_1_debug',
'-gline-tables-only -MO', src_files, boilerplate_code)
os.system('echo "// clang-format on" >> compiled/mtl_default_shaders_autogen.inc')
if __name__ == '__main__': if __name__ == '__main__':
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "common.h" #include "common.h"
constant bool kCombineWithExistingResult [[function_constant(1)]]; constant bool kCombineWithExistingResult [[function_constant(1000)]];
// Combine the visibility result of current render pass with previous value from previous render // Combine the visibility result of current render pass with previous value from previous render
// pass // pass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment