public CVMetalTexture TextureFromImage(CVImageBuffer imageBuffer, MTLPixelFormat format, nint width, nint height, nint planeIndex, out CVReturn errorCode)
        {
            if (imageBuffer == null)
            {
                throw new ArgumentNullException("imageBuffer");
            }

            IntPtr texture;

            errorCode = CVMetalTextureCacheCreateTextureFromImage(
                allocator:    IntPtr.Zero,
                textureCache: handle,                 /* textureCache dict, one day we might add it */
                sourceImage:  imageBuffer.Handle,
                textureAttr:  IntPtr.Zero,
                format:       (nuint)(ulong)format,
                width:        width,
                height:       height,
                planeIndex:   planeIndex,
                textureOut:   out texture);
            if (errorCode != 0)
            {
                return(null);
            }
            return(new CVMetalTexture(texture));
        }
示例#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);
        }
示例#3
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;
        }
示例#4
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);
		}
示例#5
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;
		}
示例#6
0
        public MTLPipeline(ref GraphicsPipelineDescription description, MTLGraphicsDevice gd)
            : base(ref description)
        {
            PrimitiveType        = MTLFormats.VdToMTLPrimitiveTopology(description.PrimitiveTopology);
            ResourceLayouts      = new MTLResourceLayout[description.ResourceLayouts.Length];
            NonVertexBufferCount = 0;
            for (int i = 0; i < ResourceLayouts.Length; i++)
            {
                ResourceLayouts[i]    = Util.AssertSubtype <ResourceLayout, MTLResourceLayout>(description.ResourceLayouts[i]);
                NonVertexBufferCount += ResourceLayouts[i].BufferCount;
            }
            ResourceBindingModel = description.ResourceBindingModel ?? gd.ResourceBindingModel;

            CullMode           = MTLFormats.VdToMTLCullMode(description.RasterizerState.CullMode);
            FrontFace          = MTLFormats.VdVoMTLFrontFace(description.RasterizerState.FrontFace);
            FillMode           = MTLFormats.VdToMTLFillMode(description.RasterizerState.FillMode);
            ScissorTestEnabled = description.RasterizerState.ScissorTestEnabled;

            MTLRenderPipelineDescriptor mtlDesc = MTLRenderPipelineDescriptor.New();

            foreach (Shader shader in description.ShaderSet.Shaders)
            {
                MTLShader   mtlShader = Util.AssertSubtype <Shader, MTLShader>(shader);
                MTLFunction specializedFunction;

                if (mtlShader.HasFunctionConstants)
                {
                    // Need to create specialized MTLFunction.
                    MTLFunctionConstantValues constantValues = CreateConstantValues(description.ShaderSet.Specializations);
                    specializedFunction = mtlShader.Library.newFunctionWithNameConstantValues(mtlShader.EntryPoint, constantValues);
                    AddSpecializedFunction(specializedFunction);
                    ObjectiveCRuntime.release(constantValues.NativePtr);

                    Debug.Assert(specializedFunction.NativePtr != IntPtr.Zero, "Failed to create specialized MTLFunction");
                }
                else
                {
                    specializedFunction = mtlShader.Function;
                }

                if (shader.Stage == ShaderStages.Vertex)
                {
                    mtlDesc.vertexFunction = specializedFunction;
                }
                else if (shader.Stage == ShaderStages.Fragment)
                {
                    mtlDesc.fragmentFunction = specializedFunction;
                }
            }

            // Vertex layouts
            VertexLayoutDescription[] vdVertexLayouts  = description.ShaderSet.VertexLayouts;
            MTLVertexDescriptor       vertexDescriptor = mtlDesc.vertexDescriptor;

            for (uint i = 0; i < vdVertexLayouts.Length; i++)
            {
                uint layoutIndex = ResourceBindingModel == ResourceBindingModel.Improved
                    ? NonVertexBufferCount + i
                    : i;
                MTLVertexBufferLayoutDescriptor mtlLayout = vertexDescriptor.layouts[layoutIndex];
                mtlLayout.stride = (UIntPtr)vdVertexLayouts[i].Stride;
                uint stepRate = vdVertexLayouts[i].InstanceStepRate;
                mtlLayout.stepFunction = stepRate == 0 ? MTLVertexStepFunction.PerVertex : MTLVertexStepFunction.PerInstance;
                mtlLayout.stepRate     = (UIntPtr)Math.Max(1, stepRate);
            }

            uint element = 0;

            for (uint i = 0; i < vdVertexLayouts.Length; i++)
            {
                uint offset = 0;
                VertexLayoutDescription vdDesc = vdVertexLayouts[i];
                for (uint j = 0; j < vdDesc.Elements.Length; j++)
                {
                    VertexElementDescription     elementDesc  = vdDesc.Elements[j];
                    MTLVertexAttributeDescriptor mtlAttribute = vertexDescriptor.attributes[element];
                    mtlAttribute.bufferIndex = (UIntPtr)(ResourceBindingModel == ResourceBindingModel.Improved
                        ? NonVertexBufferCount + i
                        : i);
                    mtlAttribute.format = MTLFormats.VdToMTLVertexFormat(elementDesc.Format);
                    mtlAttribute.offset = elementDesc.Offset != 0 ? (UIntPtr)elementDesc.Offset : (UIntPtr)offset;
                    offset  += FormatHelpers.GetSizeInBytes(elementDesc.Format);
                    element += 1;
                }
            }

            VertexBufferCount = (uint)vdVertexLayouts.Length;

            // Outputs
            OutputDescription     outputs        = description.Outputs;
            BlendStateDescription blendStateDesc = description.BlendState;

            BlendColor = blendStateDesc.BlendFactor;

            if (outputs.SampleCount != TextureSampleCount.Count1)
            {
                mtlDesc.sampleCount = (UIntPtr)FormatHelpers.GetSampleCountUInt32(outputs.SampleCount);
            }

            if (outputs.DepthAttachment != null)
            {
                PixelFormat    depthFormat    = outputs.DepthAttachment.Value.Format;
                MTLPixelFormat mtlDepthFormat = MTLFormats.VdToMTLPixelFormat(depthFormat, true);
                mtlDesc.depthAttachmentPixelFormat = mtlDepthFormat;
                if ((FormatHelpers.IsStencilFormat(depthFormat)))
                {
                    HasStencil = true;
                    mtlDesc.stencilAttachmentPixelFormat = mtlDepthFormat;
                }
            }
            for (uint i = 0; i < outputs.ColorAttachments.Length; i++)
            {
                BlendAttachmentDescription attachmentBlendDesc       = blendStateDesc.AttachmentStates[i];
                MTLRenderPipelineColorAttachmentDescriptor colorDesc = mtlDesc.colorAttachments[i];
                colorDesc.pixelFormat                 = MTLFormats.VdToMTLPixelFormat(outputs.ColorAttachments[i].Format, false);
                colorDesc.blendingEnabled             = attachmentBlendDesc.BlendEnabled;
                colorDesc.alphaBlendOperation         = MTLFormats.VdToMTLBlendOp(attachmentBlendDesc.AlphaFunction);
                colorDesc.sourceAlphaBlendFactor      = MTLFormats.VdToMTLBlendFactor(attachmentBlendDesc.SourceAlphaFactor);
                colorDesc.destinationAlphaBlendFactor = MTLFormats.VdToMTLBlendFactor(attachmentBlendDesc.DestinationAlphaFactor);

                colorDesc.rgbBlendOperation         = MTLFormats.VdToMTLBlendOp(attachmentBlendDesc.ColorFunction);
                colorDesc.sourceRGBBlendFactor      = MTLFormats.VdToMTLBlendFactor(attachmentBlendDesc.SourceColorFactor);
                colorDesc.destinationRGBBlendFactor = MTLFormats.VdToMTLBlendFactor(attachmentBlendDesc.DestinationColorFactor);
            }

            RenderPipelineState = gd.Device.newRenderPipelineStateWithDescriptor(mtlDesc);
            ObjectiveCRuntime.release(mtlDesc.NativePtr);

            if (outputs.DepthAttachment != null)
            {
                MTLDepthStencilDescriptor depthDescriptor = MTLUtil.AllocInit <MTLDepthStencilDescriptor>(
                    nameof(MTLDepthStencilDescriptor));
                depthDescriptor.depthCompareFunction = MTLFormats.VdToMTLCompareFunction(
                    description.DepthStencilState.DepthComparison);
                depthDescriptor.depthWriteEnabled = description.DepthStencilState.DepthWriteEnabled;

                bool stencilEnabled = description.DepthStencilState.StencilTestEnabled;
                if (stencilEnabled)
                {
                    StencilReference = description.DepthStencilState.StencilReference;

                    StencilBehaviorDescription vdFrontDesc = description.DepthStencilState.StencilFront;
                    MTLStencilDescriptor       front       = MTLUtil.AllocInit <MTLStencilDescriptor>(nameof(MTLStencilDescriptor));
                    front.readMask                   = stencilEnabled ? description.DepthStencilState.StencilReadMask : 0u;
                    front.writeMask                  = stencilEnabled ? description.DepthStencilState.StencilWriteMask : 0u;
                    front.depthFailureOperation      = MTLFormats.VdToMTLStencilOperation(vdFrontDesc.DepthFail);
                    front.stencilFailureOperation    = MTLFormats.VdToMTLStencilOperation(vdFrontDesc.Fail);
                    front.depthStencilPassOperation  = MTLFormats.VdToMTLStencilOperation(vdFrontDesc.Pass);
                    front.stencilCompareFunction     = MTLFormats.VdToMTLCompareFunction(vdFrontDesc.Comparison);
                    depthDescriptor.frontFaceStencil = front;

                    StencilBehaviorDescription vdBackDesc = description.DepthStencilState.StencilBack;
                    MTLStencilDescriptor       back       = MTLUtil.AllocInit <MTLStencilDescriptor>(nameof(MTLStencilDescriptor));
                    back.readMask                   = stencilEnabled ? description.DepthStencilState.StencilReadMask : 0u;
                    back.writeMask                  = stencilEnabled ? description.DepthStencilState.StencilWriteMask : 0u;
                    back.depthFailureOperation      = MTLFormats.VdToMTLStencilOperation(vdBackDesc.DepthFail);
                    back.stencilFailureOperation    = MTLFormats.VdToMTLStencilOperation(vdBackDesc.Fail);
                    back.depthStencilPassOperation  = MTLFormats.VdToMTLStencilOperation(vdBackDesc.Pass);
                    back.stencilCompareFunction     = MTLFormats.VdToMTLCompareFunction(vdBackDesc.Comparison);
                    depthDescriptor.backFaceStencil = back;

                    ObjectiveCRuntime.release(front.NativePtr);
                    ObjectiveCRuntime.release(back.NativePtr);
                }

                DepthStencilState = gd.Device.newDepthStencilStateWithDescriptor(depthDescriptor);
                ObjectiveCRuntime.release(depthDescriptor.NativePtr);
            }

            DepthClipMode = description.DepthStencilState.DepthTestEnabled ? MTLDepthClipMode.Clip : MTLDepthClipMode.Clamp;
        }
 public virtual nuint HistogramSizeForSourceFormat(MTLPixelFormat sourceFormat)
 {
     return(GetHistogramSize(sourceFormat));
 }
示例#8
0
        public CVMetalTexture TextureFromImage(CVImageBuffer imageBuffer, MTLPixelFormat format, nint width, nint height, nint planeIndex, out CVReturn errorCode)
        {
            if (imageBuffer == null)
                throw new ArgumentNullException ("imageBuffer");

            IntPtr texture;
            errorCode = CVMetalTextureCacheCreateTextureFromImage (
                allocator:    IntPtr.Zero,
                textureCache: handle, /* textureCache dict, one day we might add it */
                sourceImage:  imageBuffer.Handle,
                textureAttr:  IntPtr.Zero,
                format:       (nuint) (ulong) format,
                width:        width,
                height:       height,
                planeIndex:   planeIndex,
                textureOut:   out texture);
            if (errorCode != 0)
                return null;
            return new CVMetalTexture (texture);
        }