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(); }
public void RenderWithEncoder(IMTLRenderCommandEncoder encoder) { if (diffuseTexture != null) encoder.SetFragmentTexture (diffuseTexture, (nuint)(int)TextureIndex.Diffuse); encoder.SetFragmentBuffer (materialUniforms, 0, (nuint)(int)BufferIndex.MaterialUniformBuffer); encoder.SetVertexBuffer (materialUniforms, 0, (nuint)(int)BufferIndex.MaterialUniformBuffer); // Draw the submesh. encoder.DrawIndexedPrimitives (submesh.PrimitiveType, submesh.IndexCount, submesh.IndexType, submesh.IndexBuffer.Buffer, submesh.IndexBuffer.Offset); }
public void Draw(MTKView view) { // Update var time = clock.ElapsedMilliseconds / 1000.0f; var viewProj = Matrix4.Mult(this.view, this.proj); var world = Matrix4.CreateRotationX(time) * Matrix4.CreateRotationY(time * 2) * Matrix4.CreateRotationZ(time * .7f); var worldViewProj = world * viewProj; var worldInverse = Matrix4.Invert(world); param.World = Matrix4.Transpose(world); param.WorldInverseTranspose = worldInverse; param.WorldViewProjection = Matrix4.Transpose(worldViewProj); SetConstantBuffer(this.param, 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.SetVertexBuffer(constantBuffer, (nuint)Marshal.SizeOf(this.param), 1); renderEncoder.SetFragmentBuffer(constantBuffer, (nuint)Marshal.SizeOf(this.param), 1); renderEncoder.SetFragmentTexture(this.texture, 0); renderEncoder.SetFragmentSamplerState(this.sampler, 0); // Tell the render context we want to draw our primitives renderEncoder.DrawIndexedPrimitives(MTLPrimitiveType.Triangle, (uint)indexDataArray.Length, MTLIndexType.UInt16, indexBuffer, 0); // 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 RenderWithEncoder(IMTLRenderCommandEncoder encoder) { if (diffuseTexture != null) { encoder.SetFragmentTexture(diffuseTexture, (nuint)(int)TextureIndex.Diffuse); } encoder.SetFragmentBuffer(materialUniforms, 0, (nuint)(int)BufferIndex.MaterialUniformBuffer); encoder.SetVertexBuffer(materialUniforms, 0, (nuint)(int)BufferIndex.MaterialUniformBuffer); // Draw the submesh. encoder.DrawIndexedPrimitives(submesh.PrimitiveType, submesh.IndexCount, submesh.IndexType, submesh.IndexBuffer.Buffer, submesh.IndexBuffer.Offset); }
private void PlatformSetPixelShaderStructuredBuffer(int slot, Buffer buffer) { _deviceCommandEncoder.SetFragmentBuffer(buffer?.DeviceBuffer, 0, (nuint)slot); }