Files
Seele/src/Engine/Graphics/Metal/Shader.mm
T

51 lines
1.6 KiB
Plaintext
Raw Normal View History

2024-04-11 12:38:42 +02:00
#include "Shader.h"
2024-04-13 23:51:38 +02:00
#include "Foundation/NSError.hpp"
#include "Foundation/NSString.hpp"
2024-04-11 12:38:42 +02:00
#include "Graphics.h"
2024-04-11 18:51:47 +02:00
#include "Graphics/Enums.h"
2024-04-11 12:38:42 +02:00
#include "Graphics/slang-compile.h"
2024-04-13 23:51:38 +02:00
#include "Metal/MTLDevice.hpp"
#include "Metal/MTLLibrary.hpp"
2024-04-12 09:27:30 +02:00
#include <iostream>
2024-04-11 12:38:42 +02:00
#include <slang.h>
2024-04-13 23:51:38 +02:00
#include <spirv_cross.hpp>
#include <spirv_cross/spirv_msl.hpp>
2024-04-11 12:38:42 +02:00
using namespace Seele;
using namespace Seele::Metal;
2024-04-11 18:51:47 +02:00
Shader::Shader(PGraphics graphics, Gfx::SeShaderStageFlags stage)
: stage(stage), graphics(graphics) {}
2024-04-11 12:38:42 +02:00
Shader::~Shader() {
if (function) {
function->release();
library->release();
}
}
void Shader::create(const ShaderCreateInfo &createInfo) {
2024-04-11 18:51:47 +02:00
Slang::ComPtr<slang::IBlob> kernelBlob =
2024-04-13 23:51:38 +02:00
generateShader(createInfo, SLANG_SPIRV);
spirv_cross::CompilerMSL comp((const uint32 *)kernelBlob->getBufferPointer(),
kernelBlob->getBufferSize() / 4);
auto options = comp.get_msl_options();
options.argument_buffers = true;
options.argument_buffers_tier =
spirv_cross::CompilerMSL::Options::ArgumentBuffersTier::Tier2;
options.set_msl_version(3);
comp.set_msl_options(options);
std::string metalCode = comp.compile();
NS::Error *error = nullptr;
MTL::CompileOptions *mtlOptions = MTL::CompileOptions::alloc()->init();
library = graphics->getDevice()->newLibrary(
NS::String::string(metalCode.c_str(), NS::ASCIIStringEncoding),
mtlOptions, &error);
if (error) {
std::cout << error->debugDescription() << std::endl;
assert(false);
2024-04-11 12:38:42 +02:00
}
2024-04-11 18:51:47 +02:00
function =
library->newFunction(NS::String::string("main", NS::ASCIIStringEncoding));
2024-04-13 23:51:38 +02:00
mtlOptions->release();
2024-04-11 12:38:42 +02:00
}