void SetupMetal()
        {
            // Find a usable device
            device = MTLDevice.SystemDefault;

            // Create a new command queue
            commandQueue = device.CreateCommandQueue();

            // Load all the shader files with a metal file extension in the project
            NSError error;

            defaultLibrary = device.CreateLibrary("default.metallib", out error);

            // Setup metal layer and add as sub layer to view
            metalLayer             = new CAMetalLayer();
            metalLayer.Device      = device;
            metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm;

            // Change this to NO if the compute encoder is used as the last pass on the drawable texture
            metalLayer.FramebufferOnly = true;

            // Add metal layer to the views layer hierarchy
            metalLayer.Frame = View.Layer.Frame;
            View.Layer.AddSublayer(metalLayer);

            View.Opaque             = true;
            View.BackgroundColor    = null;
            View.ContentScaleFactor = UIScreen.MainScreen.Scale;
        }
Beispiel #2
0
        public Renderer()
        {
            // initialize properties
            sampleCount        = 1;
            depthPixelFormat   = MTLPixelFormat.Depth32Float;
            stencilPixelFormat = MTLPixelFormat.Invalid;

            // find a usable Device
            device = MTLDevice.SystemDefault;

            // create a new command queue
            commandQueue = device.CreateCommandQueue();

            NSError error;

            shaderLibrary = device.CreateLibrary("default.metallib", out error);

            // if the shader libary isnt loading, nothing good will happen
            if (shaderLibrary == null)
            {
                throw new Exception("ERROR: Couldnt create a default shader library");
            }

            inflightSemaphore = new Semaphore(maxInflightBuffers, maxInflightBuffers);
        }
        public Renderer()
        {
            sampleCount        = 4;
            depthPixelFormat   = MTLPixelFormat.Depth32Float;
            stencilPixelFormat = MTLPixelFormat.Invalid;

            // find a usable Device
            device = MTLDevice.SystemDefault;

            // create a new command queue
            commandQueue = device.CreateCommandQueue();

            NSError error;

            defaultLibrary = device.CreateLibrary("default.metallib", out error);

            // if the shader libary isnt loading, nothing good will happen
            if (defaultLibrary == null)
            {
                throw new Exception("ERROR: Couldnt create a default shader library");
            }

            constantDataBufferIndex = 0;
            inflightSemaphore       = new Semaphore(max_inflight_buffers, max_inflight_buffers);

            constantBuffer = new Uniforms[2];
            constantBuffer [0].ambientColor = box1AmbientColor;
            constantBuffer [0].diffuseColor = box1DiffuseColor;

            constantBuffer [1].ambientColor = box2AmbientColor;
            constantBuffer [1].diffuseColor = box2DiffuseColor;

            multiplier = 1;
        }
Beispiel #4
0
        void InitializeShaderModule(IMTLDevice device, MgComputePipelineCreateInfo createInfo)
        {
            var stage  = createInfo.Stage;
            var module = (AmtShaderModule)stage.Module;

            Debug.Assert(module != null);
            if (module.Library == null)
            {
                using (var ms = new MemoryStream())
                {
                    module.Info.Code.CopyTo(ms, (int)module.Info.CodeSize.ToUInt32());

                    // UPDATE SHADERMODULE wIth FUNCTION FOR REUSE
                    using (NSData data = NSData.FromArray(ms.ToArray()))
                    {
                        NSError err;
                        module.Library = device.CreateLibrary(data, out err);
                        if (module.Library == null)
                        {
                            // TODO: better error handling
                            throw new Exception(err.ToString());
                        }
                    }
                }
            }
            mFunction = module.Library.CreateFunction(stage.Name);
            Debug.Assert(mFunction != null);
        }
Beispiel #5
0
        public IMTLLibrary LoadLibrary(IMTLDevice device, MemoryStream ms)
        {
            // UPDATE SHADERMODULE wIth FUNCTION FOR REUSE
            var byteArray = ms.ToArray();

            using (NSData data = NSData.FromArray(byteArray))
            {
                NSError     err;
                IMTLLibrary library = device.CreateLibrary(data, out err);
                if (library == null)
                {
                    // TODO: better error handling
                    throw new Exception(err.ToString());
                }
                return(library);
            }
        }
Beispiel #6
0
 public IMTLLibrary LoadLibrary(IMTLDevice device, MemoryStream ms)
 {
     using (var tr = new StreamReader(ms))
     {
         string source  = tr.ReadToEnd();
         var    options = new MTLCompileOptions
         {
             LanguageVersion = MTLLanguageVersion.v1_1,
             FastMathEnabled = false,
         };
         NSError     err;
         IMTLLibrary library = device.CreateLibrary(source, options, out err);
         if (library == null)
         {
             // TODO: better error handling
             throw new Exception(err.ToString());
         }
         return(library);
     }
 }
Beispiel #7
0
		public Renderer ()
		{
			// initialize properties
			sampleCount = 1;
			depthPixelFormat = MTLPixelFormat.Depth32Float;
			stencilPixelFormat = MTLPixelFormat.Invalid;

			// find a usable Device
			device = MTLDevice.SystemDefault;

			// create a new command queue
			commandQueue = device.CreateCommandQueue ();

			NSError error;
			shaderLibrary = device.CreateLibrary ("default.metallib", out error);

			// if the shader libary isnt loading, nothing good will happen
			if (shaderLibrary == null)
				throw new Exception ("ERROR: Couldnt create a default shader library");

			inflightSemaphore = new Semaphore (maxInflightBuffers, maxInflightBuffers);
		}
Beispiel #8
0
		public Renderer ()
		{
			sampleCount = 4;
			depthPixelFormat = MTLPixelFormat.Depth32Float;
			stencilPixelFormat = MTLPixelFormat.Invalid;

			// find a usable Device
			device = MTLDevice.SystemDefault;

			// create a new command queue
			commandQueue = device.CreateCommandQueue ();

			NSError error;
			defaultLibrary = device.CreateLibrary ("default.metallib", out error);

			// if the shader libary isnt loading, nothing good will happen
			if (defaultLibrary == null)
				throw new Exception ("ERROR: Couldnt create a default shader library");

			constantDataBufferIndex = 0;
			inflightSemaphore = new Semaphore (max_inflight_buffers, max_inflight_buffers);

			constantBuffer = new Uniforms[2];
			constantBuffer [0].ambientColor = box1AmbientColor;
			constantBuffer [0].diffuseColor = box1DiffuseColor;

			constantBuffer [1].ambientColor = box2AmbientColor;
			constantBuffer [1].diffuseColor = box2DiffuseColor;

			multiplier = 1;
		}
Beispiel #9
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Set the view to use the default device
            device = MTLDevice.SystemDefault;

            if (device == null)
            {
                Console.WriteLine("Metal is not supported on this device");
                View = new NSView(View.Frame);
            }

            // Create a new command queue
            commandQueue = device.CreateCommandQueue();

            NSError error;

            // Setup view
            view          = (MTKView)View;
            view.Delegate = this;
            view.Device   = device;

            view.SampleCount              = 1;
            view.DepthStencilPixelFormat  = MTLPixelFormat.Depth32Float_Stencil8;
            view.ColorPixelFormat         = MTLPixelFormat.BGRA8Unorm;
            view.PreferredFramesPerSecond = 60;
            view.ClearColor = new MTLClearColor(0, 0, 0, 1.0f);

            // Functions
            var source = System.IO.File.ReadAllText("Triangle.metal");
            MTLCompileOptions compileOptions = new MTLCompileOptions()
            {
                LanguageVersion = MTLLanguageVersion.v2_0,
            };

            IMTLLibrary  customLibrary    = device.CreateLibrary(source, compileOptions, out error);
            IMTLFunction kernelFunction   = customLibrary.CreateFunction("tessellation_kernel_triangle");
            IMTLFunction vertexFunction   = customLibrary.CreateFunction("tessellation_vertex_triangle");
            IMTLFunction fragmentFunction = customLibrary.CreateFunction("tessellation_fragment");

            // Create a vertex descriptor
            MTLVertexDescriptor vertexDescriptor = new MTLVertexDescriptor();

            vertexDescriptor.Attributes[0].Format      = MTLVertexFormat.Float4;
            vertexDescriptor.Attributes[0].BufferIndex = 0;
            vertexDescriptor.Attributes[0].Offset      = 0;

            vertexDescriptor.Layouts[0].Stride = 4 * sizeof(float);

            vertexDescriptor.Layouts[0].StepRate     = 1;
            vertexDescriptor.Layouts[0].StepFunction = MTLVertexStepFunction.PerPatchControlPoint;

            // Create RenderPipeline
            var renderPipelineStateDescriptor = new MTLRenderPipelineDescriptor
            {
                SampleCount                  = view.SampleCount,
                VertexFunction               = vertexFunction,
                FragmentFunction             = fragmentFunction,
                VertexDescriptor             = vertexDescriptor,
                DepthAttachmentPixelFormat   = view.DepthStencilPixelFormat,
                StencilAttachmentPixelFormat = view.DepthStencilPixelFormat,

                MaxTessellationFactor             = 16,
                IsTessellationFactorScaleEnabled  = false,
                TessellationFactorFormat          = MTLTessellationFactorFormat.Half,
                TessellationControlPointIndexType = MTLTessellationControlPointIndexType.None,
                TessellationFactorStepFunction    = MTLTessellationFactorStepFunction.Constant,
                TessellationOutputWindingOrder    = MTLWinding.Clockwise,
                TessellationPartitionMode         = MTLTessellationPartitionMode.FractionalEven,
            };

            renderPipelineStateDescriptor.ColorAttachments[0].PixelFormat = view.ColorPixelFormat;

            renderPipelineState = device.CreateRenderPipelineState(renderPipelineStateDescriptor, out error);
            if (renderPipelineState == null)
            {
                Console.WriteLine("Failed to created pipeline state, error {0}", error);
            }

            MTLDepthStencilDescriptor depthStateDesc = new MTLDepthStencilDescriptor
            {
                DepthCompareFunction = MTLCompareFunction.Less,
                DepthWriteEnabled    = true
            };

            depthState = device.CreateDepthStencilState(depthStateDesc);

            computePipelineState = device.CreateComputePipelineState(kernelFunction, out error);

            // Buffers
            tessellationFactorsBuffer       = device.CreateBuffer(256, MTLResourceOptions.StorageModePrivate);
            tessellationFactorsBuffer.Label = "Tessellation Factors";

            controlPointsBuffer       = device.CreateBuffer(controlPointPositionsTriangle, MTLResourceOptions.StorageModeManaged);
            controlPointsBuffer.Label = "Control Points Triangle";
        }
Beispiel #10
0
        void SetupMetal()
        {
            // Find a usable device
            device = MTLDevice.SystemDefault;

            // Create a new command queue
            commandQueue = device.CreateCommandQueue ();

            // Load all the shader files with a metal file extension in the project
            NSError error;

            defaultLibrary = device.CreateLibrary ("default.metallib", out error);

            // Setup metal layer and add as sub layer to view
            metalLayer = new CAMetalLayer ();
            metalLayer.Device = device;
            metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm;

            // Change this to NO if the compute encoder is used as the last pass on the drawable texture
            metalLayer.FramebufferOnly = true;

            // Add metal layer to the views layer hierarchy
            metalLayer.Frame = View.Layer.Frame;
            View.Layer.AddSublayer (metalLayer);

            View.Opaque = true;
            View.BackgroundColor = null;
            View.ContentScaleFactor = UIScreen.MainScreen.Scale;
        }