public unsafe MTLShader(ref ShaderDescription description, MTLGraphicsDevice gd) : base(description.Stage) { _device = gd; if (description.ShaderBytes.Length > 4 && description.ShaderBytes[0] == 0x4d && description.ShaderBytes[1] == 0x54 && description.ShaderBytes[2] == 0x4c && description.ShaderBytes[3] == 0x42) { DispatchQueue queue = Dispatch.dispatch_get_global_queue(QualityOfServiceLevel.QOS_CLASS_USER_INTERACTIVE, 0); fixed(byte *shaderBytesPtr = description.ShaderBytes) { DispatchData dispatchData = Dispatch.dispatch_data_create( shaderBytesPtr, (UIntPtr)description.ShaderBytes.Length, queue, IntPtr.Zero); try { Library = gd.Device.newLibraryWithData(dispatchData); } finally { Dispatch.dispatch_release(dispatchData.NativePtr); } } } else { string source = Encoding.UTF8.GetString(description.ShaderBytes); MTLCompileOptions compileOptions = MTLCompileOptions.New(); Library = gd.Device.newLibraryWithSource(source, compileOptions); ObjectiveCRuntime.release(compileOptions); } Function = Library.newFunctionWithName(description.EntryPoint); if (Function.NativePtr == IntPtr.Zero) { throw new VeldridException( $"Failed to create Metal {description.Stage} Shader. The given entry point \"{description.EntryPoint}\" was not found."); } if (Function.functionConstantsDictionary.count != UIntPtr.Zero) { // Need to create specialized MTLFunction. ObjectiveCRuntime.release(Function.NativePtr); MTLFunctionConstantValues constantValues = MTLFunctionConstantValues.New(); Function = Library.newFunctionWithNameConstantValues(description.EntryPoint, constantValues); ObjectiveCRuntime.release(constantValues.NativePtr); Debug.Assert(Function.NativePtr != IntPtr.Zero, "Failed to create specialized MTLFunction"); } }
private unsafe MTLFunctionConstantValues CreateConstantValues(SpecializationConstant[] specializations) { MTLFunctionConstantValues ret = MTLFunctionConstantValues.New(); if (specializations != null) { foreach (SpecializationConstant sc in specializations) { MTLDataType mtlType = MTLFormats.VdVoMTLShaderConstantType(sc.Type); ret.setConstantValuetypeatIndex(&sc.Data, mtlType, (UIntPtr)sc.ID); } } return(ret); }