2024-04-11 12:38:42 +02:00
|
|
|
#include "Shader.h"
|
2024-04-19 22:44:00 +02:00
|
|
|
#include "Descriptor.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-15 13:48:34 +02:00
|
|
|
#include <fstream>
|
2024-04-12 09:27:30 +02:00
|
|
|
#include <iostream>
|
2024-04-11 12:38:42 +02:00
|
|
|
#include <slang.h>
|
2025-02-14 00:39:53 +01:00
|
|
|
#include <regex>
|
2024-04-11 12:38:42 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
using namespace Seele::Metal;
|
|
|
|
|
|
2024-08-28 17:54:14 +02:00
|
|
|
Shader::Shader(PGraphics graphics, Gfx::SeShaderStageFlags stage) : stage(stage), graphics(graphics) {}
|
2024-04-14 11:35:37 +02:00
|
|
|
|
2024-04-11 12:38:42 +02:00
|
|
|
Shader::~Shader() {
|
2024-08-28 17:54:14 +02:00
|
|
|
if (function) {
|
|
|
|
|
function->release();
|
|
|
|
|
library->release();
|
|
|
|
|
}
|
2024-04-11 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:54:14 +02:00
|
|
|
void Shader::create(const ShaderCreateInfo& createInfo) {
|
|
|
|
|
auto [kernelBlob, entryPoint] = generateShader(createInfo);
|
|
|
|
|
hash = CRC::Calculate(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), CRC::CRC_32());
|
2025-02-14 00:39:53 +01:00
|
|
|
std::regex pattern("\\[\\[buffer\\(\\d+\\)\\]\\]");
|
|
|
|
|
|
|
|
|
|
std::string codeStr = std::string((const char*)kernelBlob->getBufferPointer());
|
|
|
|
|
auto matches_begin = std::sregex_iterator(codeStr.begin(), codeStr.end(), pattern);
|
|
|
|
|
auto matches_end = std::sregex_iterator();
|
|
|
|
|
|
|
|
|
|
for (auto it = matches_begin; it != matches_end; ++it) {
|
|
|
|
|
usedDescriptors.add(std::atoi((*it).str().c_str()+9)); // [[buffer( is 9 characters
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:54:14 +02:00
|
|
|
NS::Error* error;
|
|
|
|
|
MTL::CompileOptions* options = MTL::CompileOptions::alloc()->init();
|
|
|
|
|
library = graphics->getDevice()->newLibrary(NS::String::string((char*)kernelBlob->getBufferPointer(), NS::ASCIIStringEncoding), options,
|
|
|
|
|
&error);
|
|
|
|
|
options->release();
|
|
|
|
|
if (error) {
|
|
|
|
|
std::cout << error->localizedDescription()->cString(NS::ASCIIStringEncoding) << std::endl;
|
|
|
|
|
}
|
|
|
|
|
function = library->newFunction(NS::String::string(entryPoint.c_str(), NS::ASCIIStringEncoding));
|
|
|
|
|
if (!function) {
|
|
|
|
|
assert(false);
|
|
|
|
|
}
|
2024-04-14 11:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-19 18:23:36 +02:00
|
|
|
uint32 Shader::getShaderHash() const { return hash; }
|