diff --git a/.gitignore b/.gitignore index 528462ad..817786a3 100644 --- a/.gitignore +++ b/.gitignore @@ -64,5 +64,9 @@ fb.bat *.elf *.smdh +# Compiled Metal shader files +*.ir +*.metallib + config.toml CMakeSettings.json diff --git a/CMakeLists.txt b/CMakeLists.txt index b47fc716..13be2537 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -409,26 +409,44 @@ if(ENABLE_METAL AND APPLE) include/renderer_mtl/mtl_texture.hpp include/renderer_mtl/mtl_vertex_buffer_cache.hpp include/renderer_mtl/pica_to_mtl.hpp + include/renderer_mtl/objc_helper.hpp ) set(RENDERER_MTL_SOURCE_FILES src/core/renderer_mtl/metal_cpp_impl.cpp src/core/renderer_mtl/renderer_mtl.cpp src/core/renderer_mtl/mtl_texture.cpp src/core/renderer_mtl/mtl_etc1.cpp + src/core/renderer_mtl/objc_helper.mm src/host_shaders/metal_shaders.metal ) set(HEADER_FILES ${HEADER_FILES} ${RENDERER_MTL_INCLUDE_FILES}) source_group("Source Files\\Core\\Metal Renderer" FILES ${RENDERER_MTL_SOURCE_FILES}) - # TODO: compile Metal shaders to .metallib + add_custom_command( + OUTPUT ${CMAKE_SOURCE_DIR}/src/host_shaders/metal_shaders.ir + COMMAND xcrun -sdk macosx metal -o ${CMAKE_SOURCE_DIR}/src/host_shaders/metal_shaders.ir -c ${CMAKE_SOURCE_DIR}/src/host_shaders/metal_shaders.metal + DEPENDS ${CMAKE_SOURCE_DIR}/src/host_shaders/metal_shaders.metal + VERBATIM) + + add_custom_command( + OUTPUT ${CMAKE_SOURCE_DIR}/src/host_shaders/metal_shaders.metallib + COMMAND xcrun -sdk macosx metallib -o ${CMAKE_SOURCE_DIR}/src/host_shaders/metal_shaders.metallib ${CMAKE_SOURCE_DIR}/src/host_shaders/metal_shaders.ir + DEPENDS ${CMAKE_SOURCE_DIR}/src/host_shaders/metal_shaders.ir + VERBATIM) + + add_custom_target( + compile_msl_shader + DEPENDS src/host_shaders/metal_shaders.metallib + ) cmrc_add_resource_library( resources_renderer_mtl NAMESPACE RendererMTL WHENCE "src/host_shaders/" - "src/host_shaders/metal_shaders.metal" + "src/host_shaders/metal_shaders.metallib" ) + add_dependencies(resources_renderer_mtl compile_msl_shader) target_sources(AlberCore PRIVATE ${RENDERER_MTL_SOURCE_FILES}) target_compile_definitions(AlberCore PUBLIC "PANDA3DS_ENABLE_METAL=1") diff --git a/include/renderer_mtl/objc_helper.hpp b/include/renderer_mtl/objc_helper.hpp new file mode 100644 index 00000000..04823978 --- /dev/null +++ b/include/renderer_mtl/objc_helper.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace Metal { + +dispatch_data_t createDispatchData(const void* data, size_t size); + +} // namespace Metal diff --git a/src/core/renderer_mtl/objc_helper.mm b/src/core/renderer_mtl/objc_helper.mm new file mode 100644 index 00000000..eeea56a0 --- /dev/null +++ b/src/core/renderer_mtl/objc_helper.mm @@ -0,0 +1,12 @@ +#include "renderer_mtl/objc_helper.hpp" + +// TODO: change the include +#import + +namespace Metal { + +dispatch_data_t createDispatchData(const void* data, size_t size) { + return dispatch_data_create(data, size, dispatch_get_global_queue(0, 0), ^{}); +} + +} // namespace Metal diff --git a/src/core/renderer_mtl/renderer_mtl.cpp b/src/core/renderer_mtl/renderer_mtl.cpp index b55f2de8..9fbae859 100644 --- a/src/core/renderer_mtl/renderer_mtl.cpp +++ b/src/core/renderer_mtl/renderer_mtl.cpp @@ -1,5 +1,6 @@ #include "PICA/gpu.hpp" #include "renderer_mtl/renderer_mtl.hpp" +#include "renderer_mtl/objc_helper.hpp" #include #include @@ -124,11 +125,11 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) { // Load shaders auto mtlResources = cmrc::RendererMTL::get_filesystem(); - auto shaderSource = mtlResources.open("metal_shaders.metal"); - std::string source(shaderSource.begin(), shaderSource.size()); - MTL::CompileOptions* compileOptions = MTL::CompileOptions::alloc()->init(); + auto shaderSource = mtlResources.open("metal_shaders.metallib"); + //MTL::CompileOptions* compileOptions = MTL::CompileOptions::alloc()->init(); NS::Error* error = nullptr; - MTL::Library* library = device->newLibrary(NS::String::string(source.c_str(), NS::ASCIIStringEncoding), compileOptions, &error); + MTL::Library* library = device->newLibrary(Metal::createDispatchData(shaderSource.begin(), shaderSource.size()), &error); + //MTL::Library* library = device->newLibrary(NS::String::string(source.c_str(), NS::ASCIIStringEncoding), compileOptions, &error); if (error) { Helpers::panic("Error loading shaders: %s", error->description()->cString(NS::ASCIIStringEncoding)); }