almost finish redoing metal shader compilation

This commit is contained in:
Greg Wells
2025-07-19 07:10:02 -04:00
parent eca4a144e1
commit 7d4973e27d
3 changed files with 35 additions and 57 deletions

View File

@@ -114,26 +114,26 @@ void metalDrawIndexed(gnCommandBufferHandle buffer, gnIndexType type, int indexC
void metalBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t set) {
id<MTLRenderCommandEncoder> encoder = (id<MTLRenderCommandEncoder>)buffer->commandBuffer->encoder;
for (int i = 0; i < uniform->uniform->bindingCount; i++) {
if (uniform->uniform->bindings[i].type == GN_UNIFORM_BUFFER_DESCRIPTOR) {
gnBufferUniformInfo info = *(gnBufferUniformInfo*)uniform->uniform->bindings[i].data;
[encoder setVertexBuffer:info.buffer->buffer->buffer
offset:info.offset
atIndex:buffer->commandBuffer->boundGraphcisPipeline->graphicsPipeline->vertexShaderMaps.sets[set].bindings[info.binding]
];
} else if (uniform->uniform->bindings[i].type == GN_SHADER_STORE_BUFFER_DESCRIPTOR) {
gnStorageUniformInfo info = *(gnStorageUniformInfo*)uniform->uniform->bindings[i].data;
[encoder setVertexBuffer:info.buffer->buffer->buffer
offset:info.offset
atIndex:buffer->commandBuffer->boundGraphcisPipeline->graphicsPipeline->vertexShaderMaps.sets[set].bindings[info.binding]
];
} else if (uniform->uniform->bindings[i].type == GN_IMAGE_DESCRIPTOR) {
gnImageUniformInfo info = *(gnImageUniformInfo*)uniform->uniform->bindings[i].data;
uint32_t index = buffer->commandBuffer->boundGraphcisPipeline->graphicsPipeline->fragmentShaderMaps.sets[set].bindings[info.binding];
[encoder setFragmentTexture:info.texture->texture->texture atIndex:index];
[encoder setFragmentSamplerState:info.texture->texture->sampler atIndex:index];
}
}
// for (int i = 0; i < uniform->uniform->bindingCount; i++) {
// if (uniform->uniform->bindings[i].type == GN_UNIFORM_BUFFER_DESCRIPTOR) {
// gnBufferUniformInfo info = *(gnBufferUniformInfo*)uniform->uniform->bindings[i].data;
// [encoder setVertexBuffer:info.buffer->buffer->buffer
// offset:info.offset
// atIndex:buffer->commandBuffer->boundGraphcisPipeline->graphicsPipeline->vertexShaderMaps.sets[set].bindings[info.binding]
// ];
// } else if (uniform->uniform->bindings[i].type == GN_SHADER_STORE_BUFFER_DESCRIPTOR) {
// gnStorageUniformInfo info = *(gnStorageUniformInfo*)uniform->uniform->bindings[i].data;
// [encoder setVertexBuffer:info.buffer->buffer->buffer
// offset:info.offset
// atIndex:buffer->commandBuffer->boundGraphcisPipeline->graphicsPipeline->vertexShaderMaps.sets[set].bindings[info.binding]
// ];
// } else if (uniform->uniform->bindings[i].type == GN_IMAGE_DESCRIPTOR) {
// gnImageUniformInfo info = *(gnImageUniformInfo*)uniform->uniform->bindings[i].data;
// uint32_t index = buffer->commandBuffer->boundGraphcisPipeline->graphicsPipeline->fragmentShaderMaps.sets[set].bindings[info.binding];
// [encoder setFragmentTexture:info.texture->texture->texture atIndex:index];
// [encoder setFragmentSamplerState:info.texture->texture->sampler atIndex:index];
// }
// }
}
void metalBindVertexBytes(gnCommandBufferHandle buffer, gnPushConstantLayout layout, void* data) {

View File

@@ -1,48 +1,20 @@
#include "metal_shader_compiler.h"
#include "spirv_msl.hpp"
#include "iostream"
// spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_UNIFORM_BUFFER, &list, &count);
// // [[buffer(0)]] is reserved for stage_in, [[buffer(1)]] is reserved for push_constant
// uint32_t currentBufferBinding = 2, currentTextureBinding = 0;
// for (int i = 0; i < count; i++) {
// uint32_t set = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationDescriptorSet),
// binding = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationBinding);
// spvc_compiler_unset_decoration(compiler, list[i].id, SpvDecorationBinding);
// spvc_compiler_set_decoration(compiler, list[i].id, SpvDecorationBinding, currentBufferBinding);
// module->shaderModule->map.sets[set].bindings[binding] = currentBufferBinding;
// currentBufferBinding++;
// }
// spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_SAMPLED_IMAGE, &list, &count);
// for (int i = 0; i < count; i++) {
// uint32_t set = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationDescriptorSet),
// binding = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationBinding);
// spvc_compiler_unset_decoration(compiler, list[i].id, SpvDecorationBinding);
// spvc_compiler_set_decoration(compiler, list[i].id, SpvDecorationBinding, currentTextureBinding);
// module->shaderModule->map.sets[set].bindings[binding] = currentTextureBinding;
// currentTextureBinding++;
// }
// spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_STORAGE_BUFFER, &list, &count);
// for (int i = 0; i < count; i++) {
// uint32_t set = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationDescriptorSet),
// binding = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationBinding);
// spvc_compiler_unset_decoration(compiler, list[i].id, SpvDecorationBinding);
// spvc_compiler_set_decoration(compiler, list[i].id, SpvDecorationBinding, currentBufferBinding);
// module->shaderModule->map.sets[set].bindings[binding] = currentBufferBinding;
// currentBufferBinding++;
// }
// spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_PUSH_CONSTANT, &list, &count);
// for (int i = 0; i < count; i++) {
// spvc_compiler_unset_decoration(compiler, list[i].id, SpvDecorationBinding);
// spvc_compiler_set_decoration(compiler, list[i].id, SpvDecorationBinding, 1);
// }
void handle_resources(spirv_cross::CompilerMSL& compiler, spirv_cross::SmallVector<spirv_cross::Resource>& resources) {
for (int i = 0; i < resources.size(); i++) {
uint32_t set = compiler.get_decoration(resources[i].id, spv::DecorationDescriptorSet);
compiler.unset_decoration(resources[i].id, spv::DecorationDescriptorSet);
compiler.set_decoration(resources[i].id, spv::DecorationDescriptorSet, set + 1);
}
}
extern "C" const char* mtlCompileShader(uint32_t* code, size_t wordCount, mtlShaderOptions* inOptions) {
spirv_cross::CompilerMSL compiler(code, wordCount);
spirv_cross::CompilerMSL::Options options;
options.enable_base_index_zero = false;
if (inOptions->useArgumentBuffers) {
options.set_msl_version(3);
options.argument_buffers = true;
@@ -59,6 +31,11 @@ extern "C" const char* mtlCompileShader(uint32_t* code, size_t wordCount, mtlSha
return NULL;
}
auto arg_buffers = compiler.get_shader_resources();
handle_resources(compiler, arg_buffers.uniform_buffers);
handle_resources(compiler, arg_buffers.storage_buffers);
handle_resources(compiler, arg_buffers.sampled_images);
std::string returnedCode = compiler.compile();
char* returnString = (char*)malloc(sizeof(char) * returnedCode.size());
strcpy(returnString, returnedCode.c_str());

View File

@@ -45,6 +45,7 @@ gnReturnCode createMetalShaderModule(gnShaderModule module, gnDevice device, gnS
NSString* functionName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
module->shaderModule->function = [shaderLib newFunctionWithName:functionName];
// printf("res %s\n", res);
[shaderLib release];
free((void*)res);