public override void Update(ApplicationTime time) { var timer = time.TotalMilliseconds / (3600); Camera.Update(); light.Direction.X = -14.0f + MathF.Abs(MathF.Sin(MathUtil.Radians(timer * 360.0f)) * 2.0f); light.Direction.X = 0.0f + MathF.Sin(MathUtil.Radians(timer * 360.0f)) * MathF.Cos(MathUtil.Radians(timer * 360.0f)) * 2.0f; light.Direction.Y = 0.0f + MathF.Sin(MathUtil.Radians(timer * 360.0f)) * 2.0f; light.Direction.Z = 0.0f + MathF.Cos(MathUtil.Radians(timer * 360.0f)) * 2.0f; ConstBuffer2.SetData(ref light); uniform.Update(Camera); ConstBuffer.SetData(ref uniform); Sphere.Rotation = new(yaw, -yaw, yaw); Torus.Rotation = new(-yaw, yaw, -yaw); Pyramid.Rotation = new(-yaw, -yaw, yaw); Cube.Rotation = new(yaw, -yaw, -yaw); Capsule.Rotation = new(-yaw, yaw, -yaw); yaw = timer; }
public override void Update(ApplicationTime time) { Model = Matrix4x4.CreateFromYawPitchRoll(yaw, pitch, roll) * Matrix4x4.CreateTranslation(0.0f, .0f, 0.0f); uniform.Update(Camera, Model); ConstBuffer.SetData(ref uniform); yaw += 0.0006f * MathF.PI; }
public void CreateBuffers() { VertexPositionColor[] vertices = new[] { new VertexPositionColor(new Vector3(0.0f, -0.65f, -0.5f), new Vector3(1.6f, 0.0f, 0.0f)), new VertexPositionColor(new Vector3(0.65f, 0.65f, -0.5f), new Vector3(0.0f, 1.6f, 0.0f)), new VertexPositionColor(new Vector3(-0.65f, 0.65f, -0.5f), new Vector3(0.0f, 0.0f, 1.6f)), }; int[] indices = new[] { 0, 1, 2 }; VertexBuffer = new(Device, new() { BufferFlags = BufferFlags.VertexBuffer, Usage = ResourceUsage.CPU_To_GPU, SizeInBytes = Interop.SizeOf <VertexPositionColor>(vertices), }); VertexBuffer.SetData(vertices); IndexBuffer = new(Device, new() { BufferFlags = BufferFlags.IndexBuffer, Usage = ResourceUsage.CPU_To_GPU, SizeInBytes = Interop.SizeOf <int>(indices), }); IndexBuffer.SetData(indices); ConstBuffer = new(Device, new() { BufferFlags = BufferFlags.ConstantBuffer, Usage = ResourceUsage.CPU_To_GPU, SizeInBytes = Interop.SizeOf <TransformUniform>(), }); }
public override void Update(ApplicationTime time) { var timer = time.TotalMilliseconds / (3600); Camera.Update(); //light.LightDirection.X = -14.0f + MathF.Abs(MathF.Sin(MathUtil.Radians(timer * 360.0f)) * 2.0f); light.LightDirection.X = 0.0f + MathF.Sin(MathUtil.Radians(timer * 360.0f)) * MathF.Cos(MathUtil.Radians(timer * 360.0f)) * 2.0f; light.LightDirection.Y = 0.0f + MathF.Sin(MathUtil.Radians(timer * 360.0f)) * 2.0f; light.LightDirection.Z = 0.0f + MathF.Cos(MathUtil.Radians(timer * 360.0f)) * 2.0f; ConstBuffer4.SetData(ref light); Model = Matrix4x4.CreateFromYawPitchRoll(yaw, pitch, roll) * Matrix4x4.CreateTranslation(11.0f, .3f, 0.0f); uniform.Update(Camera, Model); ConstBuffer.SetData(ref uniform); Model = Matrix4x4.CreateFromYawPitchRoll(-yaw, pitch, roll) * Matrix4x4.CreateTranslation(0, 1.0f, 0.0f); uniform.Update(Camera, Model); ConstBuffer2.SetData(ref uniform); Model = Matrix4x4.CreateFromYawPitchRoll(yaw, pitch, roll) * Matrix4x4.CreateTranslation(-11.0f, .3f, 0.0f); uniform.Update(Camera, Model); ConstBuffer3.SetData(ref uniform); if (Input.Keyboards[0].IsKeyPressed(Key.T)) { if (light.IsTexture == 1) { light.IsTexture = 0; } else if (light.IsTexture == 0) { light.IsTexture = 1; } } }
public override void Update(ApplicationTime time) { var timer = time.TotalMilliseconds / (3600); Camera.Update(); light.Direction.X = -14.0f + MathF.Abs(MathF.Sin(MathUtil.Radians(timer * 360.0f)) * 2.0f); light.Direction.X = 0.0f + MathF.Sin(MathUtil.Radians(timer * 360.0f)) * MathF.Cos(MathUtil.Radians(timer * 360.0f)) * 2.0f; light.Direction.Y = 0.0f + MathF.Sin(MathUtil.Radians(timer * 360.0f)) * 2.0f; light.Direction.Z = 0.0f + MathF.Cos(MathUtil.Radians(timer * 360.0f)) * 2.0f; ConstBuffer2.SetData(ref light); Model = Matrix4x4.CreateFromYawPitchRoll(yaw, pitch, roll) * Matrix4x4.CreateTranslation(0.0f, .0f, 0.0f); uniform.Update(Camera, Model); ConstBuffer.SetData(ref uniform); yaw = timer; }
public void SetData(byte[] data) { buffer = new Buffer(NativeDevice, new() { BufferFlags = BufferFlags.ShaderResource, SizeInBytes = Size, ByteStride = Size, Usage = ResourceUsage.CPU_To_GPU }); buffer.SetData(data); CommandBuffer cmd = new(NativeDevice, CommandBufferType.AsyncTransfer); cmd.BeginOneTimeSubmit(); VkImageSubresourceRange subresource_range = new(VkImageAspectFlags.Color, 0, (uint)MipLevels, 0, 1); VkImageMemoryBarrier memory_barrier = new() { sType = VkStructureType.ImageMemoryBarrier, pNext = null, image = handle, subresourceRange = subresource_range, srcAccessMask = VkAccessFlags.None, dstAccessMask = VkAccessFlags.TransferWrite, oldLayout = VkImageLayout.Undefined, newLayout = VkImageLayout.TransferDstOptimal, srcQueueFamilyIndex = QueueFamilyIgnored, dstQueueFamilyIndex = QueueFamilyIgnored }; cmd.PipelineBarrier(VkPipelineStageFlags.TopOfPipe, VkPipelineStageFlags.Transfer, VkDependencyFlags.None, 0, null, 0, null, memory_barrier); uint num_blits = (uint)1; int offset = 0; VkBufferImageCopy *blits = stackalloc VkBufferImageCopy[MipLevels]; // Setup buffer copy regions for each mip level. for (uint i = 0; i < num_blits; i++) { blits[i] = new() { imageSubresource = new(VkImageAspectFlags.Color, i, 0, 1), imageExtent = new(Width, Height, 1), bufferOffset = (ulong)offset }; offset += Size; } cmd.copy_buffer_to_image(handle, buffer.handle, num_blits, blits, VkImageLayout.TransferDstOptimal); VkImageMemoryBarrier memory_barrier_read = new() { sType = VkStructureType.ImageMemoryBarrier, pNext = null, image = handle, subresourceRange = subresource_range, srcAccessMask = VkAccessFlags.TransferWrite, dstAccessMask = VkAccessFlags.ShaderRead, oldLayout = VkImageLayout.TransferDstOptimal, newLayout = VkImageLayout.ShaderReadOnlyOptimal, srcQueueFamilyIndex = QueueFamilyIgnored, dstQueueFamilyIndex = QueueFamilyIgnored }; cmd.PipelineBarrier(VkPipelineStageFlags.Transfer, VkPipelineStageFlags.FragmentShader, VkDependencyFlags.None, 0, null, 0, null, memory_barrier_read); cmd.End(); Fence fence = new Fence(NativeDevice); NativeDevice.Submit(cmd, fence); fence.Wait(); //if (fence.IsSignaled) // Cleanup staging resources. fence.Dispose(); vkFreeMemory(NativeDevice.handle, buffer_memory, null); vkDestroyBuffer(NativeDevice.handle, buffer.handle, null); } public void Image2D() { buffer = new Buffer(NativeDevice, new() { BufferFlags = BufferFlags.ShaderResource, SizeInBytes = Size, ByteStride = Size, Usage = ResourceUsage.CPU_To_GPU }); buffer.SetData(Data); CommandBuffer cmd = new(NativeDevice, CommandBufferType.AsyncTransfer); cmd.BeginOneTimeSubmit(); VkImageSubresourceRange subresource_range = new(VkImageAspectFlags.Color, 0, (uint)MipLevels, 0, 1); VkImageMemoryBarrier memory_barrier = new() { sType = VkStructureType.ImageMemoryBarrier, pNext = null, image = handle, subresourceRange = subresource_range, srcAccessMask = VkAccessFlags.None, dstAccessMask = VkAccessFlags.TransferWrite, oldLayout = VkImageLayout.Undefined, newLayout = VkImageLayout.TransferDstOptimal, srcQueueFamilyIndex = QueueFamilyIgnored, dstQueueFamilyIndex = QueueFamilyIgnored }; cmd.PipelineBarrier(VkPipelineStageFlags.TopOfPipe, VkPipelineStageFlags.Transfer, VkDependencyFlags.None, 0, null, 0, null, memory_barrier); uint num_blits = (uint)1; int offset = 0; VkBufferImageCopy *blits = stackalloc VkBufferImageCopy[MipLevels]; // Setup buffer copy regions for each mip level. for (uint i = 0; i < num_blits; i++) { blits[i] = new() { imageSubresource = new(VkImageAspectFlags.Color, i, 0, 1), imageExtent = new(Width, Height, 1), bufferOffset = (ulong)offset }; offset += Size; } cmd.copy_buffer_to_image(handle, buffer.handle, num_blits, blits, VkImageLayout.TransferDstOptimal); VkImageMemoryBarrier memory_barrier_read = new() { sType = VkStructureType.ImageMemoryBarrier, pNext = null, image = handle, subresourceRange = subresource_range, srcAccessMask = VkAccessFlags.TransferWrite, dstAccessMask = VkAccessFlags.ShaderRead, oldLayout = VkImageLayout.TransferDstOptimal, newLayout = VkImageLayout.ShaderReadOnlyOptimal, srcQueueFamilyIndex = QueueFamilyIgnored, dstQueueFamilyIndex = QueueFamilyIgnored }; cmd.PipelineBarrier(VkPipelineStageFlags.Transfer, VkPipelineStageFlags.FragmentShader, VkDependencyFlags.None, 0, null, 0, null, memory_barrier_read); cmd.End(); Fence fence = new Fence(NativeDevice); NativeDevice.Submit(cmd, fence); fence.Wait(); //if (fence.IsSignaled) // Cleanup staging resources. fence.Dispose(); vkFreeMemory(NativeDevice.handle, buffer_memory, null); vkDestroyBuffer(NativeDevice.handle, buffer.handle, null); } internal VkImageView get_depth_stencil_view() { if (!IsDepthStencil) { return(VkImageView.Null); } // Create a Depth stencil view on this texture2D VkImageViewCreateInfo createInfo = new VkImageViewCreateInfo { sType = VkStructureType.ImageViewCreateInfo, flags = VkImageViewCreateFlags.None, pNext = null, viewType = VkImageViewType.Image2D, format = Format, image = handle, components = VkComponentMapping.Identity, subresourceRange = new VkImageSubresourceRange(VkImageAspectFlags.Depth | VkImageAspectFlags.Stencil, 0, 1, 0, 1) }; vkCreateImageView(NativeDevice.handle, &createInfo, null, out VkImageView _view).CheckResult(); return(_view); } internal VkImageView get_image_view() { if (!IsShaderResource) { return(VkImageView.Null); } // Create image view. VkImageViewCreateInfo imageViewCreateInfo = new VkImageViewCreateInfo() { sType = VkStructureType.ImageViewCreateInfo, image = handle, viewType = VkImageViewType.Image2D, format = Format, subresourceRange = new VkImageSubresourceRange(VkImageAspectFlags.Color, 0, 1, 0, 1), // TODO: MipMaps }; vkCreateImageView(NativeDevice.handle, &imageViewCreateInfo, null, out var _view); return(_view); }