Skip to content

Instantly share code, notes, and snippets.

@ncthbrt
Last active January 5, 2024 08:44
Show Gist options
  • Save ncthbrt/9fe33891a7a4f2a92ac2e1629c5ce9ff to your computer and use it in GitHub Desktop.
Save ncthbrt/9fe33891a7a4f2a92ac2e1629c5ce9ff to your computer and use it in GitHub Desktop.
Weird behavioural differences between cli and c api?
#include <simd/simd.h>
using namespace metal;
struct FrameData
{
float4x4 projectionMatrix;
float4x4 projectionMatrixInv;
float4x4 viewMatrix;
float4x4 viewMatrixInv;
float2 depthUnproject;
float3 screenToViewSpace;
float4x4 modelViewMatrix;
float3x3 normalMatrix;
float4x4 modelMatrix;
float3 ambientLightColor;
float3 directionalLightDirection;
float3 directionalLightColor;
uint framebufferWidth;
uint framebufferHeight;
};
struct vertexMain_out
{
float4 worldPos [[user(locn1)]];
float2 texCoord [[user(locn2)]];
float3 tangent [[user(locn3)]];
float3 bitangent [[user(locn4)]];
float3 normal [[user(locn5)]];
float4 gl_Position [[position]];
};
struct vertexMain_in
{
float4 position [[attribute(0)]];
float2 texCoord_1 [[attribute(1)]];
};
vertex vertexMain_out vertexMain(vertexMain_in in [[stage_in]])
{
vertexMain_out out = {};
out.texCoord = in.texCoord_1;
out.worldPos = in.position * frameData.modelMatrix;
return out;
}
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct FrameData
{
float4x4 projectionMatrix;
float4x4 projectionMatrixInv;
float4x4 viewMatrix;
float4x4 viewMatrixInv;
float2 depthUnproject;
float3 screenToViewSpace;
float4x4 modelViewMatrix;
float3x3 normalMatrix;
float4x4 modelMatrix;
float3 ambientLightColor;
float3 directionalLightDirection;
float3 directionalLightColor;
uint framebufferWidth;
uint framebufferHeight;
};
struct vertexMain_out
{
float4 worldPos [[user(locn1)]];
float2 texCoord [[user(locn2)]];
};
struct vertexMain_in
{
float4 position [[attribute(0)]];
float2 texCoord [[attribute(1)]];
};
vertex vertexMain_out vertexMain(vertexMain_in in [[stage_in]], constant FrameData& frameData [[buffer(0)]])
{
vertexMain_out out = {};
out.texCoord = in.texCoord;
out.worldPos = in.position * frameData.modelMatrix;
return out;
}
//
// SpirvGenerator.swift
// JelloCompiler
//
// Created by Natalie Cuthbert on 2023/12/07.
//
import Foundation
import SPIRV_Cross
import simd
enum SpirvCompilationError: Error {
case compilationError(String)
}
class SpirvCompilationErrorContext {
var errorValue: String? = nil
}
func makeMSLVersion(_ major: UInt32, _ minor: UInt32, _ patch: UInt32 = 0) -> UInt32 {
(major * 10000) + (minor * 100) + patch;
}
public func compileMSLShader(spirv: [UInt32]) throws -> String {
var context : spvc_context? = nil
var ir: spvc_parsed_ir? = nil
var compiler_msl : spvc_compiler? = nil
var result: UnsafePointer<CChar>? = nil
var options: spvc_compiler_options? = nil
var spirvData = spirv
spvc_context_create(&context)
defer {
// Frees all memory we allocated so far.
spvc_context_destroy(context);
}
let errorContext: SpirvCompilationErrorContext = .init()
let errorContextPointer = Unmanaged.passRetained(errorContext).toOpaque()
spvc_context_set_error_callback(context, { errorContext, message in
if let errorContext = errorContext, let message = message {
let ctx = Unmanaged<SpirvCompilationErrorContext>.fromOpaque(errorContext).takeRetainedValue()
ctx.errorValue = String(cString: message)
}
}, errorContextPointer
)
spvc_context_parse_spirv(context, &spirvData, spirvData.count, &ir)
if let errValue = errorContext.errorValue {
throw SpirvCompilationError.compilationError(errValue)
}
spvc_context_create_compiler(context, SPVC_BACKEND_MSL, ir, SPVC_CAPTURE_MODE_TAKE_OWNERSHIP, &compiler_msl)
if let errValue = errorContext.errorValue {
throw SpirvCompilationError.compilationError(errValue)
}
// var count = 0
// var resources: spvc_resources? = nil
// var list: UnsafePointer<spvc_reflected_resource>? = nil
// spvc_compiler_create_shader_resources(compiler_msl, &resources)
// spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_UNIFORM_BUFFER, &list, &count)
// for i in 0..<count {
// print("ID: \(list?[i].id), BaseTypeID: \(list?[i].base_type_id), TypeID: \(list?[i].type_id), Name: \(list?[i].name)");
// print("Set: \(spvc_compiler_get_decoration(compiler_msl, list![i].id, SpvDecorationDescriptorSet)), Binding: \(spvc_compiler_get_decoration(compiler_msl, list![i].id, SpvDecorationBinding))")
// }
// Modify options.
spvc_compiler_create_compiler_options(compiler_msl, &options)
if let errValue = errorContext.errorValue {
throw SpirvCompilationError.compilationError(errValue)
}
spvc_compiler_options_set_uint(options, SPVC_COMPILER_OPTION_MSL_VERSION, makeMSLVersion(2, 1, 0))
spvc_compiler_options_set_bool(options, SPVC_COMPILER_OPTION_MSL_ARGUMENT_BUFFERS, 1)
spvc_compiler_options_set_uint(options, SPVC_COMPILER_OPTION_MSL_ARGUMENT_BUFFERS_TIER, 1)
spvc_compiler_options_set_uint(options, SPVC_COMPILER_OPTION_MSL_PLATFORM, 0)
if let errValue = errorContext.errorValue {
throw SpirvCompilationError.compilationError(errValue)
}
spvc_compiler_install_compiler_options(compiler_msl, options)
if let errValue = errorContext.errorValue {
throw SpirvCompilationError.compilationError(errValue)
}
spvc_compiler_compile(compiler_msl, &result)
if let errValue = errorContext.errorValue {
throw SpirvCompilationError.compilationError(errValue)
}
let str = String(cString: result!)
return str
}
; SPIR-V
; Version: 1.6
; Generator: Khronos; 0
; Bound: 40
; Schema: 0
OpCapability Shader
%2 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Vertex %1 "vertexMain" %position %texCoord %normal %tangent %bitangent %frameData %gl_Position %worldPos %texCoord_0 %tangent_0 %bitangent_0 %normal_0
OpName %FrameData "FrameData"
OpMemberName %FrameData 0 "projectionMatrix"
OpMemberName %FrameData 1 "projectionMatrixInv"
OpMemberName %FrameData 2 "viewMatrix"
OpMemberName %FrameData 3 "viewMatrixInv"
OpMemberName %FrameData 4 "depthUnproject"
OpMemberName %FrameData 5 "screenToViewSpace"
OpMemberName %FrameData 6 "modelViewMatrix"
OpMemberName %FrameData 7 "normalMatrix"
OpMemberName %FrameData 8 "modelMatrix"
OpMemberName %FrameData 9 "ambientLightColor"
OpMemberName %FrameData 10 "directionalLightDirection"
OpMemberName %FrameData 11 "directionalLightColor"
OpMemberName %FrameData 12 "framebufferWidth"
OpMemberName %FrameData 13 "framebufferHeight"
OpName %frameData "frameData"
OpName %position "position"
OpName %texCoord "texCoord"
OpName %normal "normal"
OpName %tangent "tangent"
OpName %bitangent "bitangent"
OpName %worldPos "worldPos"
OpName %texCoord_0 "texCoord"
OpName %tangent_0 "tangent"
OpName %bitangent_0 "bitangent"
OpName %normal_0 "normal"
OpDecorate %FrameData Block
OpMemberDecorate %FrameData 0 Offset 0
OpMemberDecorate %FrameData 1 Offset 64
OpMemberDecorate %FrameData 2 Offset 128
OpMemberDecorate %FrameData 3 Offset 192
OpMemberDecorate %FrameData 4 Offset 256
OpMemberDecorate %FrameData 5 Offset 272
OpMemberDecorate %FrameData 6 Offset 288
OpMemberDecorate %FrameData 7 Offset 352
OpMemberDecorate %FrameData 8 Offset 400
OpMemberDecorate %FrameData 9 Offset 464
OpMemberDecorate %FrameData 10 Offset 480
OpMemberDecorate %FrameData 11 Offset 496
OpMemberDecorate %FrameData 12 Offset 512
OpMemberDecorate %FrameData 13 Offset 516
OpMemberDecorate %FrameData 0 MatrixStride 16
OpMemberDecorate %FrameData 1 MatrixStride 16
OpMemberDecorate %FrameData 2 MatrixStride 16
OpMemberDecorate %FrameData 3 MatrixStride 16
OpMemberDecorate %FrameData 6 MatrixStride 16
OpMemberDecorate %FrameData 7 MatrixStride 16
OpMemberDecorate %FrameData 8 MatrixStride 16
OpMemberDecorate %FrameData 0 RowMajor
OpMemberDecorate %FrameData 1 RowMajor
OpMemberDecorate %FrameData 2 RowMajor
OpMemberDecorate %FrameData 3 RowMajor
OpMemberDecorate %FrameData 6 RowMajor
OpMemberDecorate %FrameData 7 RowMajor
OpMemberDecorate %FrameData 8 RowMajor
OpDecorate %frameData DescriptorSet 2
OpDecorate %frameData Binding 3
OpDecorate %frameData NonWritable
OpDecorate %position Location 0
OpDecorate %texCoord Location 1
OpDecorate %normal Location 2
OpDecorate %tangent Location 3
OpDecorate %bitangent Location 4
OpDecorate %gl_Position BuiltIn Position
OpDecorate %worldPos Location 1
OpDecorate %texCoord_0 Location 2
OpDecorate %tangent_0 Location 3
OpDecorate %bitangent_0 Location 4
OpDecorate %normal_0 Location 5
%void = OpTypeVoid
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%mat4v4float = OpTypeMatrix %v4float 4
%v2float = OpTypeVector %float 2
%v3float = OpTypeVector %float 3
%mat3v3float = OpTypeMatrix %v3float 3
%uint = OpTypeInt 32 0
%FrameData = OpTypeStruct %mat4v4float %mat4v4float %mat4v4float %mat4v4float %v2float %v3float %mat4v4float %mat3v3float %mat4v4float %v3float %v3float %v3float %uint %uint
%_ptr_StorageBuffer_FrameData = OpTypePointer StorageBuffer %FrameData
%frameData = OpVariable %_ptr_StorageBuffer_FrameData StorageBuffer
%int = OpTypeInt 32 1
%_ptr_StorageBuffer_mat4v4float = OpTypePointer StorageBuffer %mat4v4float
%_ptr_Input_v4float = OpTypePointer Input %v4float
%_ptr_Input_v3float = OpTypePointer Input %v3float
%_ptr_Input_v2float = OpTypePointer Input %v2float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%_ptr_Output_v3float = OpTypePointer Output %v3float
%_ptr_Output_v2float = OpTypePointer Output %v2float
%position = OpVariable %_ptr_Input_v4float Input
%texCoord = OpVariable %_ptr_Input_v2float Input
%normal = OpVariable %_ptr_Input_v3float Input
%tangent = OpVariable %_ptr_Input_v3float Input
%bitangent = OpVariable %_ptr_Input_v3float Input
%gl_Position = OpVariable %_ptr_Output_v4float Output
%worldPos = OpVariable %_ptr_Output_v4float Output
%texCoord_0 = OpVariable %_ptr_Output_v2float Output
%tangent_0 = OpVariable %_ptr_Output_v3float Output
%bitangent_0 = OpVariable %_ptr_Output_v3float Output
%normal_0 = OpVariable %_ptr_Output_v3float Output
%33 = OpTypeFunction %void
%int_8 = OpConstant %int 8
%1 = OpFunction %void None %33
%34 = OpLabel
OpCopyMemory %texCoord_0 %texCoord
%35 = OpLoad %v4float %position
%36 = OpAccessChain %_ptr_StorageBuffer_mat4v4float %frameData %int_8
%37 = OpLoad %mat4v4float %36
%39 = OpMatrixTimesVector %v4float %37 %35
OpStore %worldPos %39
OpReturn
OpFunctionEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment