protected override void SetIndexBufferCore(Buffer buffer, IndexFormat format) { VkBuffer vkBuffer = Util.AssertSubtype <Buffer, VkBuffer>(buffer); vkCmdBindIndexBuffer(_cb, vkBuffer.DeviceBuffer, 0, VkFormats.VdToVkIndexFormat(format)); _referencedResources.Add(vkBuffer); }
protected override void CopyBufferCore( DeviceBuffer source, uint sourceOffset, DeviceBuffer destination, uint destinationOffset, uint sizeInBytes) { EnsureNoRenderPass(); VkBuffer srcVkBuffer = Util.AssertSubtype <DeviceBuffer, VkBuffer>(source); VkBuffer dstVkBuffer = Util.AssertSubtype <DeviceBuffer, VkBuffer>(destination); VkBufferCopy region = new VkBufferCopy { srcOffset = sourceOffset, dstOffset = destinationOffset, size = sizeInBytes }; vkCmdCopyBuffer(_cb, srcVkBuffer.DeviceBuffer, dstVkBuffer.DeviceBuffer, 1, ref region); VkMemoryBarrier barrier; barrier.sType = VkStructureType.MemoryBarrier; barrier.srcAccessMask = VkAccessFlags.TransferWrite; barrier.dstAccessMask = VkAccessFlags.VertexAttributeRead; barrier.pNext = null; vkCmdPipelineBarrier( _cb, VkPipelineStageFlags.Transfer, VkPipelineStageFlags.VertexInput, VkDependencyFlags.None, 1, ref barrier, 0, null, 0, null); }
private void UnmapBuffer(VkBuffer buffer) { if (!buffer.Memory.IsPersistentMapped) { vkUnmapMemory(_gd.Device, buffer.Memory.DeviceMemory); } }
protected override void UpdateBufferCore(DeviceBuffer buffer, uint bufferOffsetInBytes, IntPtr source, uint sizeInBytes) { VkBuffer stagingBuffer = GetStagingBuffer(sizeInBytes); _gd.UpdateBuffer(stagingBuffer, 0, source, sizeInBytes); CopyBuffer(stagingBuffer, 0, buffer, bufferOffsetInBytes, sizeInBytes); }
protected override void DrawIndexedIndirectCore(DeviceBuffer indirectBuffer, uint offset, uint drawCount, uint stride) { PreDrawCommand(); VkBuffer vkBuffer = Util.AssertSubtype <DeviceBuffer, VkBuffer>(indirectBuffer); vkCmdDrawIndexedIndirect(_cb, vkBuffer.DeviceBuffer, offset, drawCount, stride); }
protected override void DispatchIndirectCore(DeviceBuffer indirectBuffer, uint offset) { PreDispatchCommand(); VkBuffer vkBuffer = Util.AssertSubtype <DeviceBuffer, VkBuffer>(indirectBuffer); vkCmdDispatchIndirect(_cb, vkBuffer.DeviceBuffer, offset); }
protected override void DrawIndirectCore(Buffer indirectBuffer, uint offset, uint drawCount, uint stride) { PreDrawCommand(); VkBuffer vkBuffer = Util.AssertSubtype <Buffer, VkBuffer>(indirectBuffer); vkCmdDrawIndirect(_cb, vkBuffer.DeviceBuffer, offset, drawCount, stride); _referencedResources.Add(vkBuffer); }
public VkResourceSet(VkGraphicsDevice gd, ref ResourceSetDescription description) : base(ref description) { _gd = gd; VkResourceLayout vkLayout = Util.AssertSubtype <ResourceLayout, VkResourceLayout>(description.Layout); VkDescriptorSetLayout dsl = vkLayout.DescriptorSetLayout; _descriptorCounts = vkLayout.DescriptorResourceCounts; _descriptorAllocationToken = _gd.DescriptorPoolManager.Allocate(_descriptorCounts, dsl); BindableResource[] boundResources = description.BoundResources; uint descriptorWriteCount = (uint)boundResources.Length; VkWriteDescriptorSet * descriptorWrites = stackalloc VkWriteDescriptorSet[(int)descriptorWriteCount]; VkDescriptorBufferInfo *bufferInfos = stackalloc VkDescriptorBufferInfo[(int)descriptorWriteCount]; VkDescriptorImageInfo * imageInfos = stackalloc VkDescriptorImageInfo[(int)descriptorWriteCount]; for (int i = 0; i < descriptorWriteCount; i++) { VkDescriptorType type = vkLayout.DescriptorTypes[i]; descriptorWrites[i].sType = VkStructureType.WriteDescriptorSet; descriptorWrites[i].descriptorCount = 1; descriptorWrites[i].descriptorType = type; descriptorWrites[i].dstBinding = (uint)i; descriptorWrites[i].dstSet = _descriptorAllocationToken.Set; if (type == VkDescriptorType.UniformBuffer || type == VkDescriptorType.StorageBuffer) { VkBuffer vkBuffer = Util.AssertSubtype <BindableResource, VkBuffer>(boundResources[i]); bufferInfos[i].buffer = vkBuffer.DeviceBuffer; bufferInfos[i].range = vkBuffer.SizeInBytes; descriptorWrites[i].pBufferInfo = &bufferInfos[i]; } else if (type == VkDescriptorType.SampledImage) { VkTextureView textureView = Util.AssertSubtype <BindableResource, VkTextureView>(boundResources[i]); imageInfos[i].imageView = textureView.ImageView; imageInfos[i].imageLayout = VkImageLayout.ShaderReadOnlyOptimal; descriptorWrites[i].pImageInfo = &imageInfos[i]; } else if (type == VkDescriptorType.StorageImage) { VkTextureView textureView = Util.AssertSubtype <BindableResource, VkTextureView>(boundResources[i]); imageInfos[i].imageView = textureView.ImageView; imageInfos[i].imageLayout = VkImageLayout.General; descriptorWrites[i].pImageInfo = &imageInfos[i]; } else if (type == VkDescriptorType.Sampler) { VkSampler sampler = Util.AssertSubtype <BindableResource, VkSampler>(boundResources[i]); imageInfos[i].sampler = sampler.DeviceSampler; descriptorWrites[i].pImageInfo = &imageInfos[i]; } } vkUpdateDescriptorSets(_gd.Device, descriptorWriteCount, descriptorWrites, 0, null); }
protected override void SetVertexBufferCore(uint index, DeviceBuffer buffer, uint offset) { VkBuffer vkBuffer = Util.AssertSubtype <DeviceBuffer, VkBuffer>(buffer); Vulkan.VkBuffer deviceBuffer = vkBuffer.DeviceBuffer; ulong offset64 = offset; vkCmdBindVertexBuffers(_cb, index, 1, ref deviceBuffer, ref offset64); }
public override void UpdateBuffer(Buffer buffer, uint bufferOffsetInBytes, IntPtr source, uint sizeInBytes) { VkBuffer stagingBuffer = GetStagingBuffer(sizeInBytes); _gd.UpdateBuffer(stagingBuffer, 0, source, sizeInBytes); CopyBuffer(stagingBuffer, 0, buffer, bufferOffsetInBytes, sizeInBytes); _referencedResources.Add(Util.AssertSubtype <Buffer, VkBuffer>(buffer)); }
public override void SetVertexBuffer(uint index, VertexBuffer vb) { VkBuffer vkBuffer = Util.AssertSubtype <VertexBuffer, VkVertexBuffer>(vb); Vulkan.VkBuffer deviceBuffer = vkBuffer.DeviceBuffer; ulong offset = 0; vkCmdBindVertexBuffers(_cb, index, 1, ref deviceBuffer, ref offset); }
public override void UpdateBuffer(Buffer buffer, uint bufferOffsetInBytes, IntPtr source, uint sizeInBytes) { VkBuffer vkBuffer = Util.AssertSubtype <Buffer, VkBuffer>(buffer); IntPtr mappedPtr = MapBuffer(vkBuffer, sizeInBytes); byte * destPtr = (byte *)mappedPtr + bufferOffsetInBytes; Unsafe.CopyBlock(destPtr, source.ToPointer(), sizeInBytes); UnmapBuffer(vkBuffer); }
protected override void SetVertexBufferCore(uint index, Buffer buffer) { VkBuffer vkBuffer = Util.AssertSubtype <Buffer, VkBuffer>(buffer); Vulkan.VkBuffer deviceBuffer = vkBuffer.DeviceBuffer; ulong offset = 0; vkCmdBindVertexBuffers(_cb, index, 1, ref deviceBuffer, ref offset); _referencedResources.Add(vkBuffer); }
private IntPtr MapBuffer(VkBuffer buffer, uint numBytes) { if (buffer.Memory.IsPersistentMapped) { return((IntPtr)buffer.Memory.BlockMappedPointer); } else { void * mappedPtr; VkResult result = vkMapMemory(_gd.Device, buffer.Memory.DeviceMemory, buffer.Memory.Offset, numBytes, 0, &mappedPtr); CheckResult(result); return((IntPtr)mappedPtr); } }
protected override void CopyBufferCore(Buffer source, uint sourceOffset, Buffer destination, uint destinationOffset, uint sizeInBytes) { EnsureNoRenderPass(); VkBuffer srcVkBuffer = Util.AssertSubtype <Buffer, VkBuffer>(source); VkBuffer dstVkBuffer = Util.AssertSubtype <Buffer, VkBuffer>(destination); VkBufferCopy region = new VkBufferCopy { srcOffset = sourceOffset, dstOffset = destinationOffset, size = sizeInBytes }; vkCmdCopyBuffer(_cb, srcVkBuffer.DeviceBuffer, dstVkBuffer.DeviceBuffer, 1, ref region); _referencedResources.Add(srcVkBuffer); _referencedResources.Add(dstVkBuffer); }
protected override void SetIndexBufferCore(DeviceBuffer buffer, IndexFormat format, uint offset) { VkBuffer vkBuffer = Util.AssertSubtype <DeviceBuffer, VkBuffer>(buffer); vkCmdBindIndexBuffer(_cb, vkBuffer.DeviceBuffer, offset, VkFormats.VdToVkIndexFormat(format)); }
public VkResourceSet(VkGraphicsDevice gd, ref ResourceSetDescription description) : base(ref description) { _gd = gd; RefCount = new ResourceRefCount(DisposeCore); VkResourceLayout vkLayout = Util.AssertSubtype <ResourceLayout, VkResourceLayout>(description.Layout); VkDescriptorSetLayout dsl = vkLayout.DescriptorSetLayout; _descriptorCounts = vkLayout.DescriptorResourceCounts; _descriptorAllocationToken = _gd.DescriptorPoolManager.Allocate(_descriptorCounts, dsl); BindableResource[] boundResources = description.BoundResources; uint descriptorWriteCount = (uint)boundResources.Length; VkWriteDescriptorSet * descriptorWrites = stackalloc VkWriteDescriptorSet[(int)descriptorWriteCount]; VkDescriptorBufferInfo *bufferInfos = stackalloc VkDescriptorBufferInfo[(int)descriptorWriteCount]; VkDescriptorImageInfo * imageInfos = stackalloc VkDescriptorImageInfo[(int)descriptorWriteCount]; for (int i = 0; i < descriptorWriteCount; i++) { VkDescriptorType type = vkLayout.DescriptorTypes[i]; descriptorWrites[i].sType = VkStructureType.WriteDescriptorSet; descriptorWrites[i].descriptorCount = 1; descriptorWrites[i].descriptorType = type; descriptorWrites[i].dstBinding = (uint)i; descriptorWrites[i].dstSet = _descriptorAllocationToken.Set; if (type == VkDescriptorType.UniformBuffer || type == VkDescriptorType.UniformBufferDynamic || type == VkDescriptorType.StorageBuffer || type == VkDescriptorType.StorageBufferDynamic) { DeviceBufferRange range = Util.GetBufferRange(boundResources[i], 0); VkBuffer rangedVkBuffer = Util.AssertSubtype <DeviceBuffer, VkBuffer>(range.Buffer); bufferInfos[i].buffer = rangedVkBuffer.DeviceBuffer; bufferInfos[i].offset = range.Offset; bufferInfos[i].range = range.SizeInBytes; descriptorWrites[i].pBufferInfo = &bufferInfos[i]; _refCounts.Add(rangedVkBuffer.RefCount); } else if (type == VkDescriptorType.SampledImage) { TextureView texView = Util.GetTextureView(_gd, boundResources[i]); VkTextureView vkTexView = Util.AssertSubtype <TextureView, VkTextureView>(texView); imageInfos[i].imageView = vkTexView.ImageView; imageInfos[i].imageLayout = VkImageLayout.ShaderReadOnlyOptimal; descriptorWrites[i].pImageInfo = &imageInfos[i]; _sampledTextures.Add(Util.AssertSubtype <Texture, VkTexture>(texView.Target)); _refCounts.Add(vkTexView.RefCount); } else if (type == VkDescriptorType.StorageImage) { TextureView texView = Util.GetTextureView(_gd, boundResources[i]); VkTextureView vkTexView = Util.AssertSubtype <TextureView, VkTextureView>(texView); imageInfos[i].imageView = vkTexView.ImageView; imageInfos[i].imageLayout = VkImageLayout.General; descriptorWrites[i].pImageInfo = &imageInfos[i]; _storageImages.Add(Util.AssertSubtype <Texture, VkTexture>(texView.Target)); _refCounts.Add(vkTexView.RefCount); } else if (type == VkDescriptorType.Sampler) { VkSampler sampler = Util.AssertSubtype <BindableResource, VkSampler>(boundResources[i]); imageInfos[i].sampler = sampler.DeviceSampler; descriptorWrites[i].pImageInfo = &imageInfos[i]; _refCounts.Add(sampler.RefCount); } } vkUpdateDescriptorSets(_gd.Device, descriptorWriteCount, descriptorWrites, 0, null); }
public override void SetIndexBuffer(IndexBuffer ib) { VkBuffer vkBuffer = Util.AssertSubtype <IndexBuffer, VkIndexBuffer>(ib); vkCmdBindIndexBuffer(_cb, vkBuffer.DeviceBuffer, 0, VkFormats.VdToVkIndexFormat(ib.Format)); }