private static VkSampler CreateSampler(VkDevice device, uint mipLevels) { VkSamplerCreateInfo createInfo = VkSamplerCreateInfo.New(); createInfo.magFilter = VkFilter.Linear; createInfo.minFilter = VkFilter.Linear; createInfo.addressModeU = VkSamplerAddressMode.Repeat; createInfo.addressModeV = VkSamplerAddressMode.Repeat; createInfo.addressModeW = VkSamplerAddressMode.Repeat; createInfo.anisotropyEnable = VkBool32.True; createInfo.maxAnisotropy = 16; createInfo.borderColor = VkBorderColor.FloatOpaqueWhite; createInfo.unnormalizedCoordinates = VkBool32.False; createInfo.compareEnable = VkBool32.False; createInfo.compareOp = VkCompareOp.Always; createInfo.mipmapMode = VkSamplerMipmapMode.Linear; createInfo.mipLodBias = 0; createInfo.minLod = 0; createInfo.maxLod = mipLevels; VkSampler sampler = VkSampler.Null; Assert(vkCreateSampler(device, &createInfo, null, &sampler)); return(sampler); }
/// <inheritdoc/> protected internal override void OnDestroyed() { GraphicsDevice.Collect(NativeSampler); NativeSampler = VkSampler.Null; base.OnDestroyed(); }
internal static unsafe VkDescriptorSetLayout CreateNativeDescriptorSetLayout(GraphicsDevice device, IList <DescriptorSetLayoutBuilder.Entry> entries, out uint[] typeCounts) { var bindings = new VkDescriptorSetLayoutBinding[entries.Count]; var immutableSamplers = new VkSampler[entries.Count]; int usedBindingCount = 0; typeCounts = new uint[DescriptorTypeCount]; fixed(VkSampler *immutableSamplersPointer = &immutableSamplers[0]) { for (int i = 0; i < entries.Count; i++) { var entry = entries[i]; // TODO VULKAN: Special case for unused bindings in PipelineState. Handle more nicely. if (entry.ArraySize == 0) { continue; } bindings[usedBindingCount] = new VkDescriptorSetLayoutBinding { descriptorType = VulkanConvertExtensions.ConvertDescriptorType(entry.Class, entry.Type), stageFlags = VkShaderStageFlags.All, // TODO VULKAN: Filter? binding = (uint)i, descriptorCount = (uint)entry.ArraySize }; if (entry.ImmutableSampler != null) { // TODO VULKAN: Handle immutable samplers for DescriptorCount > 1 if (entry.ArraySize > 1) { throw new NotImplementedException(); } // Remember this, so we can choose the right VkDescriptorType in DescriptorSet.SetShaderResourceView immutableSamplers[i] = entry.ImmutableSampler.NativeSampler; //bindings[i].VkDescriptorType = VkDescriptorType.CombinedImageSampler; bindings[usedBindingCount].pImmutableSamplers = immutableSamplersPointer + i; } typeCounts[(int)bindings[usedBindingCount].descriptorType] += bindings[usedBindingCount].descriptorCount; usedBindingCount++; } var createInfo = new VkDescriptorSetLayoutCreateInfo { sType = VkStructureType.DescriptorSetLayoutCreateInfo, bindingCount = (uint)usedBindingCount, pBindings = usedBindingCount > 0 ? (VkDescriptorSetLayoutBinding *)Core.Interop.Fixed(bindings) : null, }; vkCreateDescriptorSetLayout(device.NativeDevice, &createInfo, null, out var descriptorSetLayout); return(descriptorSetLayout); } }
public static VkDescriptorImageInfo descriptorImageInfo(VkSampler sampler, VkImageView imageView, VkImageLayout imageLayout) { VkDescriptorImageInfo descriptorImageInfo = new VkDescriptorImageInfo(); descriptorImageInfo.sampler = sampler; descriptorImageInfo.imageView = imageView; descriptorImageInfo.imageLayout = imageLayout; return(descriptorImageInfo); }
unsafe Sampler() { if (_handle != VkSampler.Null) { VulkanNative.vkDestroySampler( _device.Handle, _handle, null ); _handle = VkSampler.Null; } }
public static void SetDebugMarkerName(this VkSampler obj, Device dev, string name) { if (!dev.debugUtilsEnabled) { return; } VkDebugUtilsObjectNameInfoEXT dmo = new VkDebugUtilsObjectNameInfoEXT(VkObjectType.Sampler, (ulong)obj.Handle) { pObjectName = name.Pin() }; Utils.CheckResult(vkSetDebugUtilsObjectNameEXT(dev.VkDev, ref dmo)); name.Unpin(); }
public static void SetDebugMarkerName(this VkSampler obj, Device dev, string name) { if (!dev.DebugMarkersEnabled) { return; } VkDebugMarkerObjectNameInfoEXT dmo = new VkDebugMarkerObjectNameInfoEXT(VkDebugReportObjectTypeEXT.SamplerEXT, obj.Handle) { pObjectName = name.Pin() }; Utils.CheckResult(vkDebugMarkerSetObjectNameEXT(dev.VkDev, ref dmo)); name.Unpin(); }
void CreateTextureSampler() { var info = new VkSamplerCreateInfo(); info.magFilter = VkFilter.Nearest; info.minFilter = VkFilter.Nearest; info.addressModeU = VkSamplerAddressMode.Repeat; info.addressModeV = VkSamplerAddressMode.Repeat; info.addressModeW = VkSamplerAddressMode.Repeat; info.anisotropyEnable = true; info.maxAnisotropy = 16; info.borderColor = VkBorderColor.FloatOpaqueBlack; info.unnormalizedCoordinates = false; textureSampler = new VkSampler(device, info); }
protected override void InitializePermanent() { var cube = GeometricPrimitive.Box(1.0f, 1.0f, 1.0f); _cubeTexture = Content.LoadVulkanImage("IndustryForgedDark512.ktx"); _cubeVertices = ToDispose(VulkanBuffer.Vertex(Context, cube.Vertices)); _cubeIndices = ToDispose(VulkanBuffer.Index(Context, cube.Indices)); var sampler = CreateSampler(); _sampler = sampler; ToDispose(new ActionDisposable(() => { vkDestroySampler(Context.Device, sampler, null); })); _uniformBuffer = ToDispose(VulkanBuffer.DynamicUniform <WorldViewProjection>(Context, 1)); var descriptorSetLayout = CreateDescriptorSetLayout(); _descriptorSetLayout = descriptorSetLayout; ToDispose(new ActionDisposable(() => { vkDestroyDescriptorSetLayout(Context.Device, descriptorSetLayout, null); })); var pipelineLayout = CreatePipelineLayout(); _pipelineLayout = pipelineLayout; ToDispose(new ActionDisposable(() => { vkDestroyPipelineLayout(Context.Device, pipelineLayout, null); })); var descriptorPool = CreateDescriptorPool(); _descriptorPool = descriptorPool; ToDispose(new ActionDisposable(() => { vkDestroyDescriptorPool(Context.Device, descriptorPool, null); })); _descriptorSet = CreateDescriptorSet(_descriptorPool); // Will be freed when pool is destroyed. }
public unsafe Sampler(Image image) { _device = image.Device; var createInfo = new VkSamplerCreateInfo { sType = VkStructureType.SamplerCreateInfo, magFilter = VkFilter.Linear, minFilter = VkFilter.Linear, addressModeU = VkSamplerAddressMode.Repeat, addressModeV = VkSamplerAddressMode.Repeat, addressModeW = VkSamplerAddressMode.Repeat, anisotropyEnable = true, maxAnisotropy = 16, borderColor = VkBorderColor.IntOpaqueBlack, unnormalizedCoordinates = false, compareEnable = false, compareOp = VkCompareOp.Always, mipmapMode = VkSamplerMipmapMode.Linear, mipLodBias = 0.0f, minLod = 0.0f, maxLod = image.MipLevel }; VkSampler sampler; if (VulkanNative.vkCreateSampler( _device.Handle, &createInfo, null, &sampler ) != VkResult.Success) { throw new Exception("failed to create sampler"); } _handle = sampler; }
public FGraphicsPipeline(VkDevice device, VkPhysicalDevice physicalDevice, VkPipelineCache pipelineCache, VkRenderPass renderPass, string shaderPath, uint swapchainImageCount, FTexture textureArray) { this.device = device; this.swapchainImageCount = swapchainImageCount; this.renderPass = renderPass; desclayout = CreateDescriptorLayout(device, 1); descriptorPool = CreateDescriptorPool(device, swapchainImageCount, 7); descriptorSets = AllocateDescriptorSets(device, desclayout, descriptorPool, swapchainImageCount); sampler = CreateSampler(device, textureArray.MipLevels); for (int i = 0; i < swapchainImageCount; i++) { VkDescriptorImageInfo imageInfo = new VkDescriptorImageInfo(); imageInfo.imageLayout = VkImageLayout.ShaderReadOnlyOptimal; imageInfo.imageView = textureArray.imageView; imageInfo.sampler = sampler; VkWriteDescriptorSet[] writes = new VkWriteDescriptorSet[1]; writes[0].dstSet = descriptorSets[i]; writes[0].dstBinding = 0; writes[0].dstArrayElement = 0; writes[0].descriptorType = VkDescriptorType.CombinedImageSampler; writes[0].descriptorCount = 1; writes[0].pImageInfo = &imageInfo; fixed(VkWriteDescriptorSet *ptr = writes) vkUpdateDescriptorSets(device, (uint)writes.Length, ptr, 0, null); } uniformdata = CreateUniformBuffer(device, physicalDevice, 1); VkShaderModule vs = LoadShader(device, $"{shaderPath}.vert.spv"); VkShaderModule fs = LoadShader(device, $"{shaderPath}.frag.spv"); VkGraphicsPipelineCreateInfo pCreateInfo = VkGraphicsPipelineCreateInfo.New(); pCreateInfo.flags = VkPipelineCreateFlags.DisableOptimization; VkPipelineShaderStageCreateInfo[] shaderStages = new VkPipelineShaderStageCreateInfo[2]; shaderStages[0] = VkPipelineShaderStageCreateInfo.New(); shaderStages[0].stage = VkShaderStageFlags.Vertex; shaderStages[0].module = vs; byte[] vsFuncName = Encoding.UTF8.GetBytes("main" + char.MinValue); fixed(byte *ptr = &(vsFuncName[0])) shaderStages[0].pName = ptr; shaderStages[1] = VkPipelineShaderStageCreateInfo.New(); shaderStages[1].stage = VkShaderStageFlags.Fragment; shaderStages[1].module = fs; byte[] fsFuncName = Encoding.UTF8.GetBytes("main" + char.MinValue); fixed(byte *ptr = &(fsFuncName[0])) shaderStages[1].pName = ptr; fixed(VkPipelineShaderStageCreateInfo *ptr = shaderStages) pCreateInfo.pStages = ptr; pCreateInfo.stageCount = 2; VkVertexInputBindingDescription[] bindings = new VkVertexInputBindingDescription[4]; bindings[0] = new VkVertexInputBindingDescription(); bindings[0].binding = 0; bindings[0].stride = (uint)sizeof(VoxelRenderer.VoxelVertex); bindings[0].inputRate = VkVertexInputRate.Vertex; bindings[1] = new VkVertexInputBindingDescription(); bindings[1].binding = 1; bindings[1].stride = (uint)sizeof(VoxelRenderer.VoxelVertex); bindings[1].inputRate = VkVertexInputRate.Vertex; bindings[2] = new VkVertexInputBindingDescription(); bindings[2].binding = 2; bindings[2].stride = (uint)sizeof(VoxelRenderer.VoxelVertex); bindings[2].inputRate = VkVertexInputRate.Vertex; bindings[3] = new VkVertexInputBindingDescription(); bindings[3].binding = 3; bindings[3].stride = (uint)sizeof(VoxelRenderer.VoxelVertex); bindings[3].inputRate = VkVertexInputRate.Vertex; VkVertexInputAttributeDescription[] attribs = new VkVertexInputAttributeDescription[4]; attribs[0].binding = 0; attribs[0].location = 0; attribs[0].format = VkFormat.R32g32b32Sfloat; attribs[0].offset = 0; attribs[1].binding = 1; attribs[1].location = 1; attribs[1].format = VkFormat.R32g32b32Sfloat; attribs[1].offset = 0; attribs[2].binding = 2; attribs[2].location = 2; attribs[2].format = VkFormat.R32g32b32Sfloat; attribs[2].offset = 0; attribs[3].binding = 3; attribs[3].location = 3; attribs[3].format = VkFormat.R32Uint; attribs[3].offset = 0; VkPipelineVertexInputStateCreateInfo vertexInput = VkPipelineVertexInputStateCreateInfo.New(); fixed(VkVertexInputBindingDescription *ptr = bindings) vertexInput.pVertexBindingDescriptions = ptr; vertexInput.vertexBindingDescriptionCount = (uint)bindings.Length; fixed(VkVertexInputAttributeDescription *ptr = attribs) vertexInput.pVertexAttributeDescriptions = ptr; vertexInput.vertexAttributeDescriptionCount = (uint)attribs.Length; pCreateInfo.pVertexInputState = &vertexInput; VkPipelineInputAssemblyStateCreateInfo inputAssembly = VkPipelineInputAssemblyStateCreateInfo.New(); inputAssembly.topology = VkPrimitiveTopology.TriangleList; pCreateInfo.pInputAssemblyState = &inputAssembly; VkPipelineViewportStateCreateInfo viewportState = VkPipelineViewportStateCreateInfo.New(); viewportState.viewportCount = 1; viewportState.scissorCount = 1; pCreateInfo.pViewportState = &viewportState; VkPipelineRasterizationStateCreateInfo rasterizationState = VkPipelineRasterizationStateCreateInfo.New(); rasterizationState.lineWidth = 1; rasterizationState.frontFace = VkFrontFace.Clockwise; rasterizationState.cullMode = VkCullModeFlags.Back; rasterizationState.polygonMode = VkPolygonMode.Fill;//TODO add line debug render pCreateInfo.pRasterizationState = &rasterizationState; VkPipelineMultisampleStateCreateInfo multisampleState = VkPipelineMultisampleStateCreateInfo.New(); multisampleState.rasterizationSamples = VkSampleCountFlags.Count1; pCreateInfo.pMultisampleState = &multisampleState; VkPipelineDepthStencilStateCreateInfo depthState = VkPipelineDepthStencilStateCreateInfo.New(); depthState.depthTestEnable = VkBool32.True; depthState.depthWriteEnable = VkBool32.True; depthState.depthCompareOp = VkCompareOp.Less; pCreateInfo.pDepthStencilState = &depthState; VkPipelineColorBlendAttachmentState colourAttachment = new VkPipelineColorBlendAttachmentState(); colourAttachment.colorWriteMask = VkColorComponentFlags.R | VkColorComponentFlags.G | VkColorComponentFlags.B | VkColorComponentFlags.A; VkPipelineColorBlendStateCreateInfo colourState = VkPipelineColorBlendStateCreateInfo.New(); colourState.pAttachments = &colourAttachment; colourState.attachmentCount = 1; pCreateInfo.pColorBlendState = &colourState; VkDynamicState[] dynamicStates = new VkDynamicState[2]; dynamicStates[0] = VkDynamicState.Viewport; dynamicStates[1] = VkDynamicState.Scissor; VkPipelineDynamicStateCreateInfo dynamicState = VkPipelineDynamicStateCreateInfo.New(); dynamicState.dynamicStateCount = (uint)dynamicStates.Length; fixed(VkDynamicState *ptr = &(dynamicStates[0])) dynamicState.pDynamicStates = ptr; pCreateInfo.pDynamicState = &dynamicState; this.pipelineLayout = CreatePipelineLayout(device, desclayout); pCreateInfo.layout = this.pipelineLayout; pCreateInfo.renderPass = renderPass; pCreateInfo.subpass = 0; VkPipeline pipeline = VkPipeline.Null; Assert(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pCreateInfo, null, &pipeline)); this.pipeline = pipeline; }
public static void vkDestroySampler(VkDevice device, VkSampler sampler, VkAllocationCallbacks pAllocator) { VkPreconditions.CheckNull(device, nameof(device)); GetDevice(device).DestroySampler(sampler); }
public static VkResult vkCreateSampler(VkDevice device, VkSamplerCreateInfo pCreateInfo, VkAllocationCallbacks pAllocator, out VkSampler pSampler) { VkPreconditions.CheckNull(device, nameof(device)); return(GetDevice(device).CreateSampler(pCreateInfo, pAllocator, out pSampler)); }
public static extern VkResult CreateSampler( VkDevice device, ref VkSamplerCreateInfo pCreateInfo, IntPtr pAllocator, out VkSampler pSampler );
public static extern void DestroySampler( VkDevice device, VkSampler sampler, IntPtr pAllocator );
public override VkResult CreateSampler(VkSamplerCreateInfo pCreateInfo, VkAllocationCallbacks pAllocator, out VkSampler pSampler) { throw new NotImplementedException(); }
public override void DestroySampler(VkSampler sampler) { throw new NotImplementedException(); }
public void DestroySampler(VkSampler sampler) { vkDestroySampler(dev, sampler, IntPtr.Zero); }
public override void DestroySampler(VkSampler sampler) { ((SoftwareSampler)sampler).Destroy(); }
public override VkResult CreateSampler(VkSamplerCreateInfo pCreateInfo, VkAllocationCallbacks pAllocator, out VkSampler pSampler) { SoftwareSampler sampler = new SoftwareSampler(this, pCreateInfo); pSampler = sampler; return(VkResult.VK_SUCCESS); }
public abstract VkResult CreateSampler(VkSamplerCreateInfo pCreateInfo, VkAllocationCallbacks pAllocator, out VkSampler pSampler);
public abstract void DestroySampler(VkSampler sampler);