public void Draw(MTKView view) { // Create a new command buffer for each renderpass to the current drawable IMTLCommandBuffer commandBuffer = commandQueue.CommandBuffer(); // Call the view's completion handler which is required by the view since it will signal its semaphore and set up the next buffer var drawable = view.CurrentDrawable; // Obtain a renderPassDescriptor generated from the view's drawable textures MTLRenderPassDescriptor renderPassDescriptor = view.CurrentRenderPassDescriptor; // If we have a valid drawable, begin the commands to render into it if (renderPassDescriptor != null) { // Create a render command encoder so we can render into something IMTLRenderCommandEncoder renderEncoder = commandBuffer.CreateRenderCommandEncoder(renderPassDescriptor); // Set context state renderEncoder.SetDepthStencilState(depthState); renderEncoder.SetRenderPipelineState(pipelineState); renderEncoder.SetVertexBuffer(vertexBuffer, 0, 0); // Tell the render context we want to draw our primitives renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, 3); // We're done encoding commands renderEncoder.EndEncoding(); // Schedule a present once the framebuffer is complete using the current drawable commandBuffer.PresentDrawable(drawable); } // Finalize rendering here & push the command buffer to the GPU commandBuffer.Commit(); }
public void Draw(MTKView view) { // Update var time = clock.ElapsedMilliseconds / 1000.0f; var viewProj = Matrix4.Mult(this.view, this.proj); var worldViewProj = Matrix4.CreateRotationX(time) * Matrix4.CreateRotationY(time * 2) * Matrix4.CreateRotationZ(time * .7f) * viewProj; worldViewProj = Matrix4.Transpose(worldViewProj); int rawsize = Marshal.SizeOf <Matrix4>(); var rawdata = new byte[rawsize]; GCHandle pinnedUniforms = GCHandle.Alloc(worldViewProj, GCHandleType.Pinned); IntPtr ptr = pinnedUniforms.AddrOfPinnedObject(); Marshal.Copy(ptr, rawdata, 0, rawsize); pinnedUniforms.Free(); Marshal.Copy(rawdata, 0, constantBuffer.Contents + rawsize, rawsize); // Create a new command buffer for each renderpass to the current drawable IMTLCommandBuffer commandBuffer = commandQueue.CommandBuffer(); // Call the view's completion handler which is required by the view since it will signal its semaphore and set up the next buffer var drawable = view.CurrentDrawable; // Obtain a renderPassDescriptor generated from the view's drawable textures MTLRenderPassDescriptor renderPassDescriptor = view.CurrentRenderPassDescriptor; // If we have a valid drawable, begin the commands to render into it if (renderPassDescriptor != null) { // Create a render command encoder so we can render into something IMTLRenderCommandEncoder renderEncoder = commandBuffer.CreateRenderCommandEncoder(renderPassDescriptor); renderEncoder.SetDepthStencilState(depthState); // Set context state renderEncoder.SetRenderPipelineState(pipelineState); renderEncoder.SetVertexBuffer(vertexBuffer, 0, 0); renderEncoder.SetVertexBuffer(constantBuffer, (nuint)Marshal.SizeOf <Matrix4>(), 1); renderEncoder.SetFragmentTexture(this.texture, 0); renderEncoder.SetFragmentSamplerState(this.sampler, 0); // Tell the render context we want to draw our primitives renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, (nuint)vertexData.Length); // We're done encoding commands renderEncoder.EndEncoding(); // Schedule a present once the framebuffer is complete using the current drawable commandBuffer.PresentDrawable(drawable); } // Finalize rendering here & push the command buffer to the GPU commandBuffer.Commit(); }
private void PlatformDraw( PrimitiveType primitiveType, uint vertexStart, uint vertexCount) { _deviceCommandEncoder.DrawPrimitives( primitiveType.ToMTLPrimitiveType(), vertexStart, vertexCount); }
public void Draw(MTKView view) { // Update this.time += this.clock.ElapsedMilliseconds / 1000.0f; if (this.time > 2) { this.time = 0; clock.Restart(); this.mipmapping++; if (this.mipmapping >= this.texture.MipmapLevelCount) { this.mipmapping = 0; } SetConstantBuffer(this.mipmapping, this.constantBuffer); } // Create a new command buffer for each renderpass to the current drawable IMTLCommandBuffer commandBuffer = commandQueue.CommandBuffer(); // Call the view's completion handler which is required by the view since it will signal its semaphore and set up the next buffer var drawable = view.CurrentDrawable; // Obtain a renderPassDescriptor generated from the view's drawable textures MTLRenderPassDescriptor renderPassDescriptor = view.CurrentRenderPassDescriptor; // If we have a valid drawable, begin the commands to render into it if (renderPassDescriptor != null) { // Create a render command encoder so we can render into something IMTLRenderCommandEncoder renderEncoder = commandBuffer.CreateRenderCommandEncoder(renderPassDescriptor); // Set context state renderEncoder.SetDepthStencilState(depthState); renderEncoder.SetRenderPipelineState(pipelineState); renderEncoder.SetVertexBuffer(vertexBuffer, 0, 0); renderEncoder.SetFragmentBuffer(constantBuffer, (nuint)sizeof(float), 1); renderEncoder.SetFragmentTexture(this.texture, 0); renderEncoder.SetFragmentSamplerState(this.sampler, 0); // Tell the render context we want to draw our primitives renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, (uint)vertexData.Length); // We're done encoding commands renderEncoder.EndEncoding(); // Schedule a present once the framebuffer is complete using the current drawable commandBuffer.PresentDrawable(drawable); } // Finalize rendering here & push the command buffer to the GPU commandBuffer.Commit(); }
void RenderBox(IMTLRenderCommandEncoder renderEncoder, GameView view, nuint offset, string name) { renderEncoder.PushDebugGroup(name); // set context state renderEncoder.SetRenderPipelineState(pipelineState); renderEncoder.SetVertexBuffer(vertexBuffer, 0, 0); renderEncoder.SetVertexBuffer(dynamicConstantBuffer [constantDataBufferIndex], offset, 1); // tell the render context we want to draw our primitives renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, 36, 1); renderEncoder.PopDebugGroup(); }
void Render() { inflightSemaphore.WaitOne(); Update(); // Create a new command buffer for each renderpass to the current drawable IMTLCommandBuffer commandBuffer = commandQueue.CommandBuffer(); commandBuffer.Label = "MyCommand"; // Obtain a drawable texture for this render pass and set up the renderpass descriptor for the command encoder to render into ICAMetalDrawable drawable = GetCurrentDrawable(); SetupRenderPassDescriptorForTexture(drawable.Texture); // Create a render command encoder so we can render into something IMTLRenderCommandEncoder renderEncoder = commandBuffer.CreateRenderCommandEncoder(renderPassDescriptor); renderEncoder.Label = "MyRenderEncoder"; renderEncoder.SetDepthStencilState(depthState); // Set context state renderEncoder.PushDebugGroup("DrawCube"); renderEncoder.SetRenderPipelineState(pipelineState); renderEncoder.SetVertexBuffer(vertexBuffer, 0, 0); renderEncoder.SetVertexBuffer(dynamicConstantBuffer, (nuint)(Marshal.SizeOf(typeof(Uniforms)) * constantDataBufferIndex), 1); // Tell the render context we want to draw our primitives renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, 36, 1); renderEncoder.PopDebugGroup(); // We're done encoding commands renderEncoder.EndEncoding(); // Call the view's completion handler which is required by the view since it will signal its semaphore and set up the next buffer commandBuffer.AddCompletedHandler(buffer => { drawable.Dispose(); inflightSemaphore.Release(); }); // Schedule a present once the framebuffer is complete commandBuffer.PresentDrawable(drawable); // Finalize rendering here & push the command buffer to the GPU commandBuffer.Commit(); // The renderview assumes it can now increment the buffer index and that the previous index won't be touched until we cycle back around to the same index constantDataBufferIndex = (byte)((constantDataBufferIndex + 1) % max_inflight_buffers); }
public void Encode(IMTLRenderCommandEncoder renderEncoder) { renderEncoder.PushDebugGroup("encode quad"); renderEncoder.SetFrontFacingWinding(MTLWinding.CounterClockwise); renderEncoder.SetDepthStencilState(depthState); renderEncoder.SetRenderPipelineState(pipelineState); renderEncoder.SetVertexBuffer(transformBuffer, 0, 2); renderEncoder.SetFragmentTexture(outTexture, 0); // Encode quad vertex and texture coordinate buffers mpQuad.Encode(renderEncoder); // tell the render context we want to draw our primitives renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, 6, 1); renderEncoder.EndEncoding(); renderEncoder.PopDebugGroup(); }
void RenderBox (IMTLRenderCommandEncoder renderEncoder, GameView view, nuint offset, string name) { renderEncoder.PushDebugGroup (name); // set context state renderEncoder.SetRenderPipelineState (pipelineState); renderEncoder.SetVertexBuffer (vertexBuffer, 0, 0); renderEncoder.SetVertexBuffer (dynamicConstantBuffer [constantDataBufferIndex], offset, 1); // tell the render context we want to draw our primitives renderEncoder.DrawPrimitives (MTLPrimitiveType.Triangle, 0, 36, 1); renderEncoder.PopDebugGroup (); }
public void Draw(MTKView view) { // Update var time = clock.ElapsedMilliseconds / 1000.0f; var viewProj = Matrix4.Mult(this.view, this.proj); var worldViewProj = Matrix4.CreateRotationX(time) * Matrix4.CreateRotationY(time * 2) * Matrix4.CreateRotationZ(time * .7f) * viewProj; worldViewProj = Matrix4.Transpose(worldViewProj); this.cubeParameters.WorldViewProjection = worldViewProj; this.SetConstantBuffer(this.cubeParameters, this.cubeConstantBuffer); // Create a new command buffer for each renderpass to the current drawable IMTLCommandBuffer commandBuffer = commandQueue.CommandBuffer(); // Call the view's completion handler which is required by the view since it will signal its semaphore and set up the next buffer var drawable = view.CurrentDrawable; // Obtain a renderPassDescriptor generated from the view's drawable textures MTLRenderPassDescriptor renderPassDescriptor = view.CurrentRenderPassDescriptor; renderPassDescriptor.ColorAttachments[0].Texture = this.colorFrameBufferTexture; renderPassDescriptor.ColorAttachments[0].ClearColor = new MTLClearColor(0.5f, 0.75f, 1, 1); // If we have a valid drawable, begin the commands to render into it if (renderPassDescriptor != null) { //-------------------------------- // Draw Triangle //-------------------------------- // Create a render command encoder so we can render into something IMTLRenderCommandEncoder triangleRenderEncoder = commandBuffer.CreateRenderCommandEncoder(renderPassDescriptor); triangleRenderEncoder.SetDepthStencilState(depthStencilState); triangleRenderEncoder.SetRenderPipelineState(trianglePipelineState); triangleRenderEncoder.SetVertexBuffer(triangleVertexBuffer, 0, 0); // Tell the render context we want to draw our primitives triangleRenderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, (nuint)triangleVertexData.Length); triangleRenderEncoder.EndEncoding(); //-------------------------------- // Draw Cube //-------------------------------- renderPassDescriptor.ColorAttachments[0].Texture = drawable.Texture; renderPassDescriptor.ColorAttachments[0].ClearColor = mtkView.ClearColor; IMTLRenderCommandEncoder cubeRenderEncoder = commandBuffer.CreateRenderCommandEncoder(renderPassDescriptor); cubeRenderEncoder.SetDepthStencilState(depthStencilState); // Set context state cubeRenderEncoder.SetRenderPipelineState(cubePipelineState); cubeRenderEncoder.SetVertexBuffer(cubeVertexBuffer, 0, 0); cubeRenderEncoder.SetVertexBuffer(cubeConstantBuffer, (nuint)Marshal.SizeOf(this.cubeParameters), 1); cubeRenderEncoder.SetFragmentTexture(this.colorFrameBufferTexture, 0); cubeRenderEncoder.SetFragmentSamplerState(this.sampler, 0); // Tell the render context we want to draw our primitives cubeRenderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, (nuint)cubeVertexData.Length); // We're done encoding commands cubeRenderEncoder.EndEncoding(); // Schedule a present once the framebuffer is complete using the current drawable commandBuffer.PresentDrawable(drawable); } // Finalize rendering here & push the command buffer to the GPU commandBuffer.Commit(); }
public void Encode (IMTLRenderCommandEncoder renderEncoder) { renderEncoder.PushDebugGroup ("encode quad"); renderEncoder.SetFrontFacingWinding (MTLWinding.CounterClockwise); renderEncoder.SetDepthStencilState (depthState); renderEncoder.SetRenderPipelineState (pipelineState); renderEncoder.SetVertexBuffer (transformBuffer, 0, 2); renderEncoder.SetFragmentTexture (mpInTexture.MetalTexture, 0); // Encode quad vertex and texture coordinate buffers mpQuad.Encode (renderEncoder); // tell the render context we want to draw our primitives renderEncoder.DrawPrimitives (MTLPrimitiveType.Triangle, 0, 6, 1); renderEncoder.EndEncoding (); renderEncoder.PopDebugGroup (); }
public void Draw(MTKView view) { // Create a new command buffer for each renderpass to the current drawable IMTLCommandBuffer commandBuffer = commandQueue.CommandBuffer(); // Call the view's completion handler which is required by the view since it will signal its semaphore and set up the next buffer var drawable = view.CurrentDrawable; // Obtain a renderPassDescriptor generated from the view's drawable textures MTLRenderPassDescriptor renderPassDescriptor = view.CurrentRenderPassDescriptor; // If we have a valid drawable, begin the commands to render into it if (renderPassDescriptor != null) { // Create a render command encoder so we can render into something IMTLRenderCommandEncoder renderEncoder = commandBuffer.CreateRenderCommandEncoder(renderPassDescriptor); // First Draw var time = clock.ElapsedMilliseconds / 1000.0f; var viewProj = Matrix4.Mult(this.view, this.proj); var worldViewProj = Matrix4.CreateRotationX(time) * Matrix4.CreateRotationY(time * 2) * Matrix4.CreateRotationZ(time * .7f) * viewProj; param.WorldViewProjection = Matrix4.Transpose(worldViewProj); param.IsTextured = true; SetConstantBuffer(this.param, this.constantBuffer1); // Set context state renderEncoder.SetRenderPipelineState(pipelineState); renderEncoder.SetFrontFacingWinding(MTLWinding.Clockwise); renderEncoder.SetCullMode(MTLCullMode.None); renderEncoder.SetStencilReferenceValue(0); renderEncoder.SetDepthStencilState(depthStencilState1); renderEncoder.SetVertexBuffer(vertexBuffer, 0, 0); renderEncoder.SetFragmentTexture(this.texture, 0); renderEncoder.SetFragmentSamplerState(this.sampler, 0); renderEncoder.SetVertexBuffer(constantBuffer1, (nuint)Marshal.SizeOf(this.param), 1); renderEncoder.SetFragmentBuffer(constantBuffer1, (nuint)Marshal.SizeOf(this.param), 1); // Tell the render context we want to draw our primitives renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, (nuint)vertexData.Length); // Second Draw // Set constant buffer param.IsTextured = false; worldViewProj = Matrix4.CreateRotationX(time) * Matrix4.CreateRotationY(time * 2) * Matrix4.CreateRotationZ(time * .7f) * Matrix4.Scale(1.04f) * viewProj; param.WorldViewProjection = Matrix4.Transpose(worldViewProj); SetConstantBuffer(this.param, constantBuffer2); renderEncoder.SetStencilReferenceValue(1); renderEncoder.SetDepthStencilState(depthStencilState2); renderEncoder.SetVertexBuffer(constantBuffer2, (nuint)Marshal.SizeOf(this.param), 1); renderEncoder.SetFragmentBuffer(constantBuffer2, (nuint)Marshal.SizeOf(this.param), 1); // Tell the render context we want to draw our primitives renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, (nuint)vertexData.Length); // We're done encoding commands renderEncoder.EndEncoding(); // Schedule a present once the framebuffer is complete using the current drawable commandBuffer.PresentDrawable(drawable); } // Finalize rendering here & push the command buffer to the GPU commandBuffer.Commit(); }