compile shaders to a metallib file

This commit is contained in:
Samuliak 2024-07-05 17:49:04 +02:00
parent 5741de2cad
commit 3005468b3f
5 changed files with 50 additions and 6 deletions

4
.gitignore vendored
View file

@ -64,5 +64,9 @@ fb.bat
*.elf
*.smdh
# Compiled Metal shader files
*.ir
*.metallib
config.toml
CMakeSettings.json

View file

@ -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")

View file

@ -0,0 +1,9 @@
#pragma once
#include <Metal/Metal.hpp>
namespace Metal {
dispatch_data_t createDispatchData(const void* data, size_t size);
} // namespace Metal

View file

@ -0,0 +1,12 @@
#include "renderer_mtl/objc_helper.hpp"
// TODO: change the include
#import <Metal/Metal.h>
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

View file

@ -1,5 +1,6 @@
#include "PICA/gpu.hpp"
#include "renderer_mtl/renderer_mtl.hpp"
#include "renderer_mtl/objc_helper.hpp"
#include <cmrc/cmrc.hpp>
#include <cstddef>
@ -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));
}