kong + metal test

This commit is contained in:
luboslenco
2025-04-16 20:22:07 +02:00
parent 93a7981715
commit a8a023cf0f
11 changed files with 111 additions and 80 deletions
-1
View File
@@ -28,7 +28,6 @@ struct iron_gpu_texture;
- (void)hideKeyboard;
- (CAMetalLayer *)metalLayer;
- (id<MTLDevice>)metalDevice;
- (id<MTLLibrary>)metalLibrary;
- (id<MTLCommandQueue>)metalQueue;
@end
-8
View File
@@ -299,10 +299,6 @@ static bool shiftDown = false;
return device;
}
- (id<MTLLibrary>)metalLibrary {
return library;
}
- (id<MTLCommandQueue>)metalQueue {
return commandQueue;
}
@@ -342,10 +338,6 @@ id getMetalDevice(void) {
return [glView metalDevice];
}
id getMetalLibrary(void) {
return [glView metalLibrary];
}
id getMetalQueue(void) {
return [glView metalQueue];
}
-1
View File
@@ -19,7 +19,6 @@ struct iron_gpu_texture;
- (CAMetalLayer *)metalLayer;
- (id<MTLDevice>)metalDevice;
- (id<MTLLibrary>)metalLibrary;
- (id<MTLCommandQueue>)metalQueue;
- (void)keyDown:(NSEvent *)theEvent;
-8
View File
@@ -412,10 +412,6 @@ static bool controlKeyMouseButton = false;
return device;
}
- (id<MTLLibrary>)metalLibrary {
return library;
}
- (id<MTLCommandQueue>)metalQueue {
return commandQueue;
}
@@ -1107,10 +1103,6 @@ id getMetalDevice(void) {
return [view metalDevice];
}
id getMetalLibrary(void) {
return [view metalLibrary];
}
id getMetalQueue(void) {
return [view metalQueue];
}
+2
View File
@@ -33,6 +33,8 @@ typedef struct iron_gpu_sampler_impl {
typedef struct {
char name[1024];
void *mtlFunction;
char *source;
int length;
} gpu_shader_impl_t;
typedef struct {
+23 -36
View File
@@ -36,7 +36,6 @@ extern bool iron_internal_metal_has_depth;
id getMetalDevice(void);
id getMetalQueue(void);
id getMetalEncoder(void);
id getMetalLibrary(void);
bool iron_internal_current_render_target_has_depth(void);
static iron_gpu_raytrace_acceleration_structure_t *accel;
static iron_gpu_raytrace_pipeline_t *pipeline;
@@ -640,6 +639,20 @@ static int findAttributeIndex(NSArray<MTLVertexAttribute *> *attributes, const c
}
void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline) {
id<MTLDevice> device = getMetalDevice();
NSError *error = nil;
id<MTLLibrary> library = [device newLibraryWithSource:[[NSString alloc] initWithBytes:pipeline->vertex_shader->impl.source length:pipeline->vertex_shader->impl.length encoding:NSUTF8StringEncoding] options:nil error:&error];
if (library == nil) {
iron_error("%s", error.localizedDescription.UTF8String);
}
pipeline->vertex_shader->impl.mtlFunction = (__bridge_retained void *)[library newFunctionWithName:[NSString stringWithCString:pipeline->vertex_shader->impl.name encoding:NSUTF8StringEncoding]];
assert(pipeline->vertex_shader->impl.mtlFunction);
pipeline->fragment_shader->impl.mtlFunction = (__bridge_retained void *)[library newFunctionWithName:[NSString stringWithCString:pipeline->fragment_shader->impl.name encoding:NSUTF8StringEncoding]];
assert(pipeline->fragment_shader->impl.mtlFunction);
MTLRenderPipelineDescriptor *renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init];
renderPipelineDesc.vertexFunction = (__bridge id<MTLFunction>)pipeline->vertex_shader->impl.mtlFunction;
renderPipelineDesc.fragmentFunction = (__bridge id<MTLFunction>)pipeline->fragment_shader->impl.mtlFunction;
@@ -713,7 +726,6 @@ void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline) {
NSError *errors = nil;
MTLRenderPipelineReflection *reflection = nil;
id<MTLDevice> device = getMetalDevice();
pipeline->impl._pipeline = (__bridge_retained void *)[device newRenderPipelineStateWithDescriptor:renderPipelineDesc
options:MTLPipelineOptionBufferTypeInfo
@@ -903,45 +915,20 @@ void iron_gpu_shader_destroy(iron_gpu_shader_t *shader) {
shader->impl.mtlFunction = NULL;
}
void iron_gpu_shader_init(iron_gpu_shader_t *shader, const void *source, size_t length, iron_gpu_shader_type_t type) {
void iron_gpu_shader_init(iron_gpu_shader_t *shader, const void *data, size_t length, iron_gpu_shader_type_t type) {
shader->impl.name[0] = 0;
const char *source = data;
{
uint8_t *data = (uint8_t *)source;
if (length > 1 && data[0] == '>') {
memcpy(shader->impl.name, data + 1, length - 1);
shader->impl.name[length - 1] = 0;
}
else {
for (int i = 3; i < length; ++i) {
if (data[i] == '\n') {
shader->impl.name[i - 3] = 0;
break;
}
else {
shader->impl.name[i - 3] = data[i];
}
}
for (int i = 3; i < length; ++i) { // //>
if (source[i] == '\n') {
shader->impl.name[i - 3] = 0;
break;
}
shader->impl.name[i - 3] = source[i];
}
char *data = (char *)source;
id<MTLLibrary> library = nil;
if (length > 1 && data[0] == '>') {
library = getMetalLibrary();
}
else {
id<MTLDevice> device = getMetalDevice();
NSError *error = nil;
library = [device newLibraryWithSource:[[NSString alloc] initWithBytes:data length:length encoding:NSUTF8StringEncoding] options:nil error:&error];
if (library == nil) {
iron_error("%s", error.localizedDescription.UTF8String);
}
}
shader->impl.mtlFunction = (__bridge_retained void *)[library newFunctionWithName:[NSString stringWithCString:shader->impl.name
encoding:NSUTF8StringEncoding]];
assert(shader->impl.mtlFunction);
shader->impl.source = data;
shader->impl.length = length;
}
bool iron_gpu_raytrace_supported() {
+27 -6
View File
@@ -360,7 +360,8 @@ unsigned char *iron_deflate_raw(unsigned char *data, int data_len, int *out_len,
#include "sinfl.h"
#endif
#if defined(IDLE_SLEEP) && !defined(IRON_WINDOWS)
#include <unistd.h>
// #include <unistd.h>
int usleep(unsigned int usec);
#endif
#ifdef IRON_MACOS
@@ -1001,6 +1002,10 @@ extern name_id names_index;
extern size_t sets_count;
extern type_id next_type_index;
void hlsl_export2(char **vs, char **fs, api_kind d3d, bool debug);
extern size_t vertex_inputs_size;
extern size_t fragment_inputs_size;
extern size_t vertex_functions_size;
extern size_t fragment_functions_size;
void gpu_create_shaders_from_kong(char *kong, char **vs, char **fs) {
next_variable_id = 1;
@@ -1010,6 +1015,10 @@ void gpu_create_shaders_from_kong(char *kong, char **vs, char **fs) {
names_index = 1;
sets_count = 0;
next_type_index = 0;
vertex_inputs_size = 0;
fragment_inputs_size = 0;
vertex_functions_size = 0;
fragment_functions_size = 0;
names_init();
types_init();
functions_init();
@@ -1024,12 +1033,25 @@ void gpu_create_shaders_from_kong(char *kong, char **vs, char **fs) {
}
analyze();
// vulkan
// transform(TRANSFORM_FLAG_ONE_COMPONENT_SWIZZLE);
#ifdef _WIN32
hlsl_export2(vs, fs, API_DIRECT3D11, false);
// metal_export(output);
#elif defined(__APPLE__)
static char vs_temp[1024 * 128];
strcpy(vs_temp, "//>kong_vert\n");
char *metal = metal_export("");
strcat(vs_temp, metal);
*vs = &vs_temp[0];
*fs = "//>kong_frag\n";
#else
// transform(TRANSFORM_FLAG_ONE_COMPONENT_SWIZZLE);
// spirv_export(output);
#endif
}
iron_gpu_shader_t *gpu_create_shader_from_source(string_t *source, iron_gpu_shader_type_t shader_type) {
@@ -1057,8 +1079,7 @@ iron_gpu_shader_t *gpu_create_shader_from_source(string_t *source, iron_gpu_shad
#elif defined(IRON_METAL)
strcpy(temp_string_s, "// my_main\n");
strcat(temp_string_s, source);
strcpy(temp_string_s, source);
shader = (iron_gpu_shader_t *)malloc(sizeof(iron_gpu_shader_t));
iron_gpu_shader_init(shader, temp_string_s, strlen(temp_string_s), shader_type);
+1
View File
@@ -166,6 +166,7 @@ function shader_context_type_size(t: string): i32 {
if (t == "vec4") return 16;
if (t == "mat3") return 48;
if (t == "mat4") return 64;
return 0;
}
function shader_context_type_pad(offset: i32, size: i32): i32 {
+55 -17
View File
@@ -63,6 +63,10 @@ extern name_id names_index;
extern size_t sets_count;
extern type_id next_type_index;
void hlsl_export2(char **vs, char **fs, api_kind d3d, bool debug);
extern size_t vertex_inputs_size;
extern size_t fragment_inputs_size;
extern size_t vertex_functions_size;
extern size_t fragment_functions_size;
void kong_compile(const char *from, const char *to) {
FILE *fp = fopen(from, "rb");
@@ -81,6 +85,10 @@ void kong_compile(const char *from, const char *to) {
names_index = 1;
sets_count = 0;
next_type_index = 0;
vertex_inputs_size = 0;
fragment_inputs_size = 0;
vertex_functions_size = 0;
fragment_functions_size = 0;
names_init();
types_init();
functions_init();
@@ -94,35 +102,32 @@ void kong_compile(const char *from, const char *to) {
}
analyze();
// vulkan
// transform(TRANSFORM_FLAG_ONE_COMPONENT_SWIZZLE);
#ifdef _WIN32
char *vs;
char *fs;
hlsl_export2(&vs, &fs, API_DIRECT3D11, false);
int i = string_last_index_of(to, "\\");
char filename[512];
strcpy(filename, to);
char *file = &filename[i + 1];
int j = string_index_of(file, ".");
file[j] = '\0';
////
// int i = string_last_index_of(to, "\\");
// char filename[512];
// strcpy(filename, to);
// char *filebase = &filename[i + 1];
// int j = string_index_of(filebase, ".");
// filebase[j] = '\0';
// char tmp[512];
// strcpy(tmp, to);
// tmp[i] = '\0';
// strcat(tmp, "\\..\\..\\temp\\");
// strcat(tmp, file);
// strcat(tmp, filebase);
// strcat(tmp, ".vert.hlsl");
// fp = fopen(tmp, "wb");
// fwrite(vs, 1, strlen(vs), fp);
// fclose(fp);
// tmp[i] = '\0';
// strcat(tmp, "\\..\\..\\temp\\");
// strcat(tmp, file);
// strcat(tmp, filebase);
// strcat(tmp, ".frag.hlsl");
// fp = fopen(tmp, "wb");
// fwrite(fs, 1, strlen(fs), fp);
@@ -140,19 +145,52 @@ void kong_compile(const char *from, const char *to) {
strcat(to_, "frag.d3d11");
hlsl_to_bin(fs, "frag", to_);
#else
#elif defined(__APPLE__)
char *metal = metal_export("");
int i = string_last_index_of(to, "/");
output[i] = '\0';
char filename[512];
strcpy(filename, to);
char *filebase = &filename[i + 1];
int j = string_index_of(filebase, ".");
filebase[j] = '\0';
// metal_export(output);
spirv_export(output);
char to_[512];
strcpy(to_, to);
to_[strlen(to_) - 5] = '\0';
strcat(to_, "vert.metal");
fp = fopen(to_, "wb");
fwrite("//>", 1, 3, fp);
fwrite(filebase, 1, strlen(filebase), fp);
fwrite("_vert\n", 1, 6, fp);
fwrite(metal, 1, strlen(metal), fp);
fclose(fp);
strcpy(to_, to);
to_[strlen(to_) - 5] = '\0';
strcat(to_, "frag.metal");
fp = fopen(to_, "wb");
fwrite("//>", 1, 3, fp);
fwrite(filebase, 1, strlen(filebase), fp);
fwrite("_frag\n", 1, 6, fp);
fclose(fp);
#else
// transform(TRANSFORM_FLAG_ONE_COMPONENT_SWIZZLE);
// int i = string_last_index_of(to, "/");
// output[i] = '\0';
// spirv_export2(output);
#endif
}
int ashader(char *shader_lang, char *from, char *to) {
// shader_lang == hlsl || msl || spirv
// shader_lang == hlsl || metal || spirv
kong_compile(from, to);
return 0;
}
+1 -1
View File
@@ -45,7 +45,7 @@ if (platform === "windows") {
project.add_lib("dxguid");
}
else if (platform === "macos") {
project.add_cfiles("../../sources/backends/mac.plist");
project.add_cfiles("../../sources/backends/data/mac.plist");
}
return project;
+2 -2
View File
@@ -560,7 +560,7 @@ class VisualStudioExporter extends Exporter {
return file.file.endsWith(".cpp") || file.file.endsWith(".c") || file.file.endsWith(".cc");
});
this.item_group(from, to, project, 'CustomBuild', (file) => {
return file.file.endsWith(".hlsl") || file.file.endsWith(".glsl");
return file.file.endsWith(".hlsl");
});
if (platform === "windows") {
this.item_group(from, to, project, "ResourceCompile", (file) => {
@@ -2433,7 +2433,7 @@ function shader_find_type(options) {
return 'spirv';
}
else if (options.graphics === 'metal') {
return 'msl';
return 'metal';
}
else if (options.graphics === 'direct3d12') {
return 'hlsl';