void setupDescriptorSetLayout() { FixedArray2<VkDescriptorSetLayoutBinding> setLayoutBindings = new FixedArray2<VkDescriptorSetLayoutBinding>( // Binding 0 : Vertex shader uniform buffer Initializers.descriptorSetLayoutBinding( VkDescriptorType.UniformBuffer, VkShaderStageFlags.Vertex, 0), // Binding 1 : Fragment shader image sampler Initializers.descriptorSetLayoutBinding( VkDescriptorType.CombinedImageSampler, VkShaderStageFlags.Fragment, 1)); VkDescriptorSetLayoutCreateInfo descriptorLayout = Initializers.descriptorSetLayoutCreateInfo( &setLayoutBindings.First, setLayoutBindings.Count); Util.CheckResult(vkCreateDescriptorSetLayout(device, &descriptorLayout, null, out descriptorSetLayout)); var dsl = descriptorSetLayout; VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = Initializers.pipelineLayoutCreateInfo( &dsl, 1); Util.CheckResult(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, null, out pipelineLayout)); }
void setupDescriptorSet() { var dsl = descriptorSetLayout; VkDescriptorSetAllocateInfo allocInfo = Initializers.descriptorSetAllocateInfo( descriptorPool, &dsl, 1); Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSet)); VkDescriptorImageInfo texDescriptor = Initializers.descriptorImageInfo( textures_colorMap.sampler, textures_colorMap.view, VkImageLayout.General); var temp = uniformBuffers_scene.descriptor; FixedArray2 <VkWriteDescriptorSet> writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>( // Binding 0 : Vertex shader uniform buffer Initializers.writeDescriptorSet( descriptorSet, VkDescriptorType.UniformBuffer, 0, &temp), // Binding 1 : Color map Initializers.writeDescriptorSet( descriptorSet, VkDescriptorType.CombinedImageSampler, 1, &texDescriptor)); vkUpdateDescriptorSets(device, (writeDescriptorSets.Count), ref writeDescriptorSets.First, 0, null); }
protected override void buildCommandBuffers() { VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo(); FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>(); clearValues.First.color = defaultClearColor; clearValues.Second.depthStencil = new VkClearDepthStencilValue { depth = 1.0f, stencil = 0 }; VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo(); renderPassBeginInfo.renderPass = renderPass; renderPassBeginInfo.renderArea.offset.x = 0; renderPassBeginInfo.renderArea.offset.y = 0; renderPassBeginInfo.renderArea.extent.width = width; renderPassBeginInfo.renderArea.extent.height = height; renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.pClearValues = &clearValues.First; for (int i = 0; i < drawCmdBuffers.Count; ++i) { // Set target frame buffer renderPassBeginInfo.framebuffer = frameBuffers[i]; Util.CheckResult(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo)); vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); VkViewport viewport = Initializers.viewport((float)width, (float)height, 0.0f, 1.0f); vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); VkRect2D scissor = Initializers.rect2D(width, height, 0, 0); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); ulong offsets = 0; // 3D scene vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts_scene, 0, 1, ref descriptorSets_scene, 0, null); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_phongPass); vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref models_example.vertices.buffer, &offsets); vkCmdBindIndexBuffer(drawCmdBuffers[i], models_example.indices.buffer, 0, VK_INDEX_TYPE_UINT32); vkCmdDrawIndexed(drawCmdBuffers[i], models_example.indexCount, 1, 0, 0, 0); // Fullscreen triangle (clipped to a quad) with radial blur if (blur) { vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts_radialBlur, 0, 1, ref descriptorSets_radialBlur, 0, null); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, (displayTexture) ? pipelines_offscreenDisplay : pipelines_radialBlur); vkCmdDraw(drawCmdBuffers[i], 3, 1, 0, 0); } vkCmdEndRenderPass(drawCmdBuffers[i]); Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i])); } }
void setupDescriptorSets() { // Image descriptor for the cube map texture VkDescriptorImageInfo textureDescriptor = Initializers.descriptorImageInfo( cubeMap.sampler, cubeMap.view, cubeMap.imageLayout); var dsl = descriptorSetLayout; VkDescriptorSetAllocateInfo allocInfo = Initializers.descriptorSetAllocateInfo( descriptorPool, &dsl, 1); // 3D object descriptor set Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSets_object)); var bufferInfo = uniformBuffers_object.descriptor; FixedArray2 <VkWriteDescriptorSet> writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet> ( // Binding 0 : Vertex shader uniform buffer Initializers.writeDescriptorSet( descriptorSets_object, VkDescriptorType.UniformBuffer, 0, &bufferInfo), // Binding 1 : Fragment shader cubemap sampler Initializers.writeDescriptorSet( descriptorSets_object, VkDescriptorType.CombinedImageSampler, 1, &textureDescriptor)); vkUpdateDescriptorSets(device, writeDescriptorSets.Count, ref writeDescriptorSets.First, 0, null); // Sky box descriptor set Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSets_skybox)); var descriptor = uniformBuffers_skybox.descriptor; writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet> ( // Binding 0 : Vertex shader uniform buffer Initializers.writeDescriptorSet( descriptorSets_skybox, VkDescriptorType.UniformBuffer, 0, &descriptor), // Binding 1 : Fragment shader cubemap sampler Initializers.writeDescriptorSet( descriptorSets_skybox, VkDescriptorType.CombinedImageSampler, 1, &textureDescriptor)); vkUpdateDescriptorSets(device, writeDescriptorSets.Count, ref writeDescriptorSets.First, 0, null); }
protected override void buildCommandBuffers() { VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo(); FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>(); clearValues.First.color = defaultClearColor; clearValues.Second.depthStencil = new VkClearDepthStencilValue { depth = 1.0f, stencil = 0 }; VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo(); renderPassBeginInfo.renderPass = renderPass; renderPassBeginInfo.renderArea.offset.x = 0; renderPassBeginInfo.renderArea.offset.y = 0; renderPassBeginInfo.renderArea.extent.width = width; renderPassBeginInfo.renderArea.extent.height = height; renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.pClearValues = &clearValues.First; for (int i = 0; i < drawCmdBuffers.Count; ++i) { renderPassBeginInfo.framebuffer = frameBuffers[i]; Util.CheckResult(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo)); vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); VkViewport viewport = Initializers.viewport((float)width, (float)height, 0.0f, 1.0f); vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); VkRect2D scissor = Initializers.rect2D(width, height, 0, 0); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); ulong offsets = 0; vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref vertexBuffer.buffer, &offsets); vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32); // Render multiple objects using different model matrices by dynamically offsetting into one uniform buffer for (uint j = 0; j < OBJECT_INSTANCES; j++) { // One dynamic offset per dynamic descriptor to offset into the ubo containing all model matrices uint dynamicOffset = j * (uint)(dynamicAlignment); // Bind the descriptor set for rendering a mesh using the dynamic offset vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, ref descriptorSet, 1, &dynamicOffset); vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0); } vkCmdEndRenderPass(drawCmdBuffers[i]); Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i])); } }
// Sets up the command buffer that renders the scene to the offscreen frame buffer void buildOffscreenCommandBuffer() { if (offscreenPass.commandBuffer == NullHandle) { offscreenPass.commandBuffer = createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false); } if (offscreenPass.semaphore == NullHandle) { VkSemaphoreCreateInfo semaphoreCreateInfo = Initializers.semaphoreCreateInfo(); Util.CheckResult(vkCreateSemaphore(device, &semaphoreCreateInfo, null, out offscreenPass.semaphore)); } VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo(); FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>(); clearValues.First.color = new VkClearColorValue { float32_0 = 0.0f, float32_1 = 0.0f, float32_2 = 0.0f, float32_3 = 0.0f }; clearValues.Second.depthStencil = new VkClearDepthStencilValue { depth = 1.0f, stencil = 0 }; VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo(); renderPassBeginInfo.renderPass = offscreenPass.renderPass; renderPassBeginInfo.framebuffer = offscreenPass.frameBuffer; renderPassBeginInfo.renderArea.extent.width = offscreenPass.width; renderPassBeginInfo.renderArea.extent.height = offscreenPass.height; renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.pClearValues = &clearValues.First; Util.CheckResult(vkBeginCommandBuffer(offscreenPass.commandBuffer, &cmdBufInfo)); VkViewport viewport = Initializers.viewport((float)offscreenPass.width, (float)offscreenPass.height, 0.0f, 1.0f); vkCmdSetViewport(offscreenPass.commandBuffer, 0, 1, &viewport); VkRect2D scissor = Initializers.rect2D(offscreenPass.width, offscreenPass.height, 0, 0); vkCmdSetScissor(offscreenPass.commandBuffer, 0, 1, &scissor); vkCmdBeginRenderPass(offscreenPass.commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBindDescriptorSets(offscreenPass.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts_scene, 0, 1, ref descriptorSets_scene, 0, null); vkCmdBindPipeline(offscreenPass.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_colorPass); ulong offsets = 0; vkCmdBindVertexBuffers(offscreenPass.commandBuffer, VERTEX_BUFFER_BIND_ID, 1, ref models_example.vertices.buffer, &offsets); vkCmdBindIndexBuffer(offscreenPass.commandBuffer, models_example.indices.buffer, 0, VK_INDEX_TYPE_UINT32); vkCmdDrawIndexed(offscreenPass.commandBuffer, models_example.indexCount, 1, 0, 0, 0); vkCmdEndRenderPass(offscreenPass.commandBuffer); Util.CheckResult(vkEndCommandBuffer(offscreenPass.commandBuffer)); }
protected override void buildCommandBuffers() { VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo(); FixedArray2<VkClearValue> clearValues = new FixedArray2<VkClearValue>(); clearValues.First.color = defaultClearColor; clearValues.Second.depthStencil = new VkClearDepthStencilValue { depth = 1.0f, stencil = 0 }; VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo(); renderPassBeginInfo.renderPass = renderPass; renderPassBeginInfo.renderArea.offset.x = 0; renderPassBeginInfo.renderArea.offset.y = 0; renderPassBeginInfo.renderArea.extent.width = width; renderPassBeginInfo.renderArea.extent.height = height; renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.pClearValues = &clearValues.First; for (int i = 0; i < drawCmdBuffers.Count; ++i) { // Set target frame buffer renderPassBeginInfo.framebuffer = frameBuffers[i]; Util.CheckResult(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo)); vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VkSubpassContents.Inline); VkViewport viewport = Initializers.viewport((float)width, (float)height, 0.0f, 1.0f); vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); VkRect2D scissor = Initializers.rect2D(width, height, 0, 0); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); ulong offsets = 0; // Skybox if (displaySkybox) { vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSets_skybox, 0, null); vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref models_skybox.vertices.buffer, ref offsets); vkCmdBindIndexBuffer(drawCmdBuffers[i], models_skybox.indices.buffer, 0, VkIndexType.Uint32); vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_skybox); vkCmdDrawIndexed(drawCmdBuffers[i], models_skybox.indexCount, 1, 0, 0, 0); } // 3D object vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSets_object, 0, null); var vbuffer = models_objects[(int)models_objectIndex].vertices.buffer; vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vbuffer, ref offsets); vkCmdBindIndexBuffer(drawCmdBuffers[i], models_objects[(int)models_objectIndex].indices.buffer, 0, VkIndexType.Uint32); vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_reflect); vkCmdDrawIndexed(drawCmdBuffers[i], models_objects[(int)models_objectIndex].indexCount, 1, 0, 0, 0); vkCmdEndRenderPass(drawCmdBuffers[i]); Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i])); } }
void setupDescriptorSet() { VkDescriptorSetAllocateInfo descriptorSetAllocInfo; // Scene rendering var dsl = descriptorSetLayouts_scene; descriptorSetAllocInfo = Initializers.descriptorSetAllocateInfo(descriptorPool, &dsl, 1); Util.CheckResult(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, out descriptorSets_scene)); var descriptor0 = uniformBuffers_scene.descriptor; var descriptor1 = textures_gradient.descriptor; NativeList <VkWriteDescriptorSet> offScreenWriteDescriptorSets = new NativeList <VkWriteDescriptorSet> { // Binding 0: Vertex shader uniform buffer Initializers.writeDescriptorSet( descriptorSets_scene, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &descriptor0), // Binding 1: Color gradient sampler Initializers.writeDescriptorSet( descriptorSets_scene, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &descriptor1), }; vkUpdateDescriptorSets(device, offScreenWriteDescriptorSets.Count, offScreenWriteDescriptorSets.Data, 0, null); // Fullscreen radial blur dsl = descriptorSetLayouts_radialBlur; descriptorSetAllocInfo = Initializers.descriptorSetAllocateInfo(descriptorPool, &dsl, 1); Util.CheckResult(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, out descriptorSets_radialBlur)); descriptor0 = uniformBuffers_blurParams.descriptor; descriptor1 = offscreenPass.descriptor; NativeList <VkWriteDescriptorSet> writeDescriptorSets = new NativeList <VkWriteDescriptorSet> { // Binding 0: Vertex shader uniform buffer Initializers.writeDescriptorSet( descriptorSets_radialBlur, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &descriptor0), // Binding 0: Fragment shader texture sampler Initializers.writeDescriptorSet( descriptorSets_radialBlur, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &descriptor1), }; vkUpdateDescriptorSets(device, writeDescriptorSets.Count, writeDescriptorSets.Data, 0, null); }
protected void createCommandBuffers() { // Create one command buffer for each swap chain image and reuse for rendering drawCmdBuffers.Resize(Swapchain.ImageCount); drawCmdBuffers.Count = Swapchain.ImageCount; VkCommandBufferAllocateInfo cmdBufAllocateInfo = Initializers.CommandBufferAllocateInfo(cmdPool, VkCommandBufferLevel.Primary, drawCmdBuffers.Count); Util.CheckResult(vkAllocateCommandBuffers(device, ref cmdBufAllocateInfo, (VkCommandBuffer *)drawCmdBuffers.Data)); }
void setupDescriptorSetLayout() { NativeList <VkDescriptorSetLayoutBinding> setLayoutBindings; VkDescriptorSetLayoutCreateInfo descriptorLayout; VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo; // Scene rendering setLayoutBindings = new NativeList <VkDescriptorSetLayoutBinding> { // Binding 0: Vertex shader uniform buffer Initializers.descriptorSetLayoutBinding( VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0), // Binding 1: Fragment shader image sampler Initializers.descriptorSetLayoutBinding( VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1), // Binding 2: Fragment shader uniform buffer Initializers.descriptorSetLayoutBinding( VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 2) }; descriptorLayout = Initializers.descriptorSetLayoutCreateInfo((VkDescriptorSetLayoutBinding *)setLayoutBindings.Data, setLayoutBindings.Count); Util.CheckResult(vkCreateDescriptorSetLayout(device, &descriptorLayout, null, out descriptorSetLayouts_scene)); var dsl = descriptorSetLayouts_scene; pPipelineLayoutCreateInfo = Initializers.pipelineLayoutCreateInfo(&dsl, 1); Util.CheckResult(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, null, out pipelineLayouts_scene)); // Fullscreen radial blur setLayoutBindings = new NativeList <VkDescriptorSetLayoutBinding> { // Binding 0 : Vertex shader uniform buffer Initializers.descriptorSetLayoutBinding( VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 0), // Binding 0: Fragment shader image sampler Initializers.descriptorSetLayoutBinding( VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1) }; descriptorLayout = Initializers.descriptorSetLayoutCreateInfo((VkDescriptorSetLayoutBinding *)setLayoutBindings.Data, setLayoutBindings.Count); Util.CheckResult(vkCreateDescriptorSetLayout(device, &descriptorLayout, null, out descriptorSetLayouts_radialBlur)); dsl = descriptorSetLayouts_radialBlur; pPipelineLayoutCreateInfo = Initializers.pipelineLayoutCreateInfo(&dsl, 1); Util.CheckResult(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, null, out pipelineLayouts_radialBlur)); }
protected override void buildCommandBuffers() { VkCommandBufferBeginInfo cmdBufInfo = Initializers.CommandBufferBeginInfo(); FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>(); clearValues.First.color = defaultClearColor; clearValues.Second.depthStencil = new VkClearDepthStencilValue() { depth = 1.0f, stencil = 0 }; VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo(); renderPassBeginInfo.renderPass = RenderPass; renderPassBeginInfo.renderArea.offset.x = 0; renderPassBeginInfo.renderArea.offset.y = 0; renderPassBeginInfo.renderArea.extent.width = Width; renderPassBeginInfo.renderArea.extent.height = Height; renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.pClearValues = &clearValues.First; for (int i = 0; i < DrawCmdBuffers.Count; ++i) { // Set target frame buffer renderPassBeginInfo.framebuffer = Framebuffers[i]; Util.CheckResult(vkBeginCommandBuffer(DrawCmdBuffers[i], &cmdBufInfo)); vkCmdBeginRenderPass(DrawCmdBuffers[i], &renderPassBeginInfo, VkSubpassContents.Inline); VkViewport viewport = Initializers.viewport((float)Width, (float)Height, 0.0f, 1.0f); vkCmdSetViewport(DrawCmdBuffers[i], 0, 1, &viewport); VkRect2D scissor = Initializers.rect2D(Width, Height, 0, 0); vkCmdSetScissor(DrawCmdBuffers[i], 0, 1, &scissor); vkCmdBindDescriptorSets(DrawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSet, 0, null); vkCmdBindPipeline(DrawCmdBuffers[i], VkPipelineBindPoint.Graphics, wireframe ? pipelines_wireframe : pipelines_solid); ulong offsets = 0; // Bind mesh vertex buffer vkCmdBindVertexBuffers(DrawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref model_vertices_buffer, ref offsets); // Bind mesh index buffer vkCmdBindIndexBuffer(DrawCmdBuffers[i], model_indices_buffer, 0, VkIndexType.Uint32); // Render mesh vertex buffer using it's indices vkCmdDrawIndexed(DrawCmdBuffers[i], (uint)model_indices_count, 1, 0, 0, 0); vkCmdEndRenderPass(DrawCmdBuffers[i]); Util.CheckResult(vkEndCommandBuffer(DrawCmdBuffers[i])); } }
void setupDescriptorPool() { // Example uses one ubo VkDescriptorPoolSize poolSizes = Initializers.descriptorPoolSize(VkDescriptorType.UniformBuffer, 1); VkDescriptorPoolCreateInfo descriptorPoolInfo = Initializers.descriptorPoolCreateInfo( 1, &poolSizes, 2); Util.CheckResult(vkCreateDescriptorPool(device, &descriptorPoolInfo, null, out descriptorPool)); }
void setupDescriptorPool() { FixedArray2<VkDescriptorPoolSize> poolSizes = new FixedArray2<VkDescriptorPoolSize>( Initializers.descriptorPoolSize(VkDescriptorType.UniformBuffer, 2), Initializers.descriptorPoolSize(VkDescriptorType.CombinedImageSampler, 2)); VkDescriptorPoolCreateInfo descriptorPoolInfo = Initializers.descriptorPoolCreateInfo( poolSizes.Count, &poolSizes.First, 2); Util.CheckResult(vkCreateDescriptorPool(device, &descriptorPoolInfo, null, out descriptorPool)); }
void setupDescriptorPool() { FixedArray2 <VkDescriptorPoolSize> poolSizes = new FixedArray2 <VkDescriptorPoolSize>( Initializers.descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), Initializers.descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1)); VkDescriptorPoolCreateInfo descriptorPoolInfo = Initializers.descriptorPoolCreateInfo( (uint)(poolSizes.Count), &poolSizes.First, 1); Util.CheckResult(vkCreateDescriptorPool(device, &descriptorPoolInfo, null, out descriptorPool)); }
void setupDescriptorPool() { // Example uses one ubo and one combined image sampler FixedArray2 <VkDescriptorPoolSize> poolSizes = new FixedArray2 <VkDescriptorPoolSize>( Initializers.descriptorPoolSize(VkDescriptorType.UniformBuffer, 1), Initializers.descriptorPoolSize(VkDescriptorType.CombinedImageSampler, 1)); VkDescriptorPoolCreateInfo descriptorPoolInfo = Initializers.descriptorPoolCreateInfo( poolSizes.Count, &poolSizes.First, 1); Util.CheckResult(vkCreateDescriptorPool(Device, &descriptorPoolInfo, null, out descriptorPool)); }
void setupVertexDescriptions() { // Binding description vertices_bindingDescriptions.Count = 1; vertices_bindingDescriptions[0] = Initializers.vertexInputBindingDescription( VERTEX_BUFFER_BIND_ID, (uint)sizeof(Vertex), VkVertexInputRate.Vertex); // Attribute descriptions // Describes memory layout and shader positions vertices_attributeDescriptions.Count = 4; // Location 0 : Position vertices_attributeDescriptions[0] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 0, VkFormat.R32g32b32Sfloat, Vertex.PositionOffset); // Location 1 : Normal vertices_attributeDescriptions[1] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 1, VkFormat.R32g32b32Sfloat, Vertex.NormalOffset); // Location 2 : Texture coordinates vertices_attributeDescriptions[2] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 2, VkFormat.R32g32Sfloat, Vertex.UvOffset); // Location 3 : Color vertices_attributeDescriptions[3] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 3, VkFormat.R32g32b32Sfloat, Vertex.ColorOffset); vertices_inputState = Initializers.pipelineVertexInputStateCreateInfo(); vertices_inputState.vertexBindingDescriptionCount = (vertices_bindingDescriptions.Count); vertices_inputState.pVertexBindingDescriptions = (VkVertexInputBindingDescription *)vertices_bindingDescriptions.Data; vertices_inputState.vertexAttributeDescriptionCount = (vertices_attributeDescriptions.Count); vertices_inputState.pVertexAttributeDescriptions = (VkVertexInputAttributeDescription *)vertices_attributeDescriptions.Data; }
void setupDescriptorPool() { // Example uses one ubo and one image sampler FixedArray3 <VkDescriptorPoolSize> poolSizes = new FixedArray3 <VkDescriptorPoolSize>( Initializers.descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), Initializers.descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1), Initializers.descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1)); VkDescriptorPoolCreateInfo descriptorPoolInfo = Initializers.descriptorPoolCreateInfo( (uint)(poolSizes.Count), &poolSizes.First, 2); Util.CheckResult(vkCreateDescriptorPool(device, &descriptorPoolInfo, null, out descriptorPool)); }
void setupDescriptorPool() { // Example uses one ubo and one image sampler FixedArray2 <VkDescriptorPoolSize> poolSizes = new FixedArray2 <VkDescriptorPoolSize>( Initializers.descriptorPoolSize(VkDescriptorType.UniformBuffer, 1), Initializers.descriptorPoolSize(VkDescriptorType.CombinedImageSampler, 1) ); VkDescriptorPoolCreateInfo descriptorPoolInfo = Initializers.descriptorPoolCreateInfo( poolSizes.Count, (VkDescriptorPoolSize *)Unsafe.AsPointer(ref poolSizes), 2); Util.CheckResult(vkCreateDescriptorPool(device, &descriptorPoolInfo, null, out descriptorPool)); }
void updateDynamicUniformBuffer(bool force = false) { // Update at max. 60 fps animationTimer += (frameTimer * 100.0f); if ((animationTimer <= 1.0f / 60.0f) && (!force)) { return; } // Dynamic ubo with per-object model matrices indexed by offsets in the command buffer uint dim = (uint)(Math.Pow(OBJECT_INSTANCES, (1.0f / 3.0f))); Vector3 offset = new Vector3(5.0f); for (uint x = 0; x < dim; x++) { for (uint y = 0; y < dim; y++) { for (uint z = 0; z < dim; z++) { uint index = x * dim * dim + y * dim + z; // Aligned offset Matrix4x4 *modelMat = (Matrix4x4 *)(((ulong)uboDataDynamic_model + (index * (ulong)dynamicAlignment))); // Update rotations rotations[index] += animationTimer * rotationSpeeds[index]; // Update matrices Vector3 pos = new Vector3(-((dim * offset.X) / 2.0f) + offset.X / 2.0f + x * offset.X, -((dim * offset.Y) / 2.0f) + offset.Y / 2.0f + y * offset.Y, -((dim * offset.Z) / 2.0f) + offset.Z / 2.0f + z * offset.Z); * modelMat = Matrix4x4.CreateTranslation(pos); * modelMat = Matrix4x4.CreateRotationX(Util.DegreesToRadians(rotations[index].X)) * *modelMat; * modelMat = Matrix4x4.CreateRotationY(Util.DegreesToRadians(rotations[index].Y)) * *modelMat; * modelMat = Matrix4x4.CreateRotationZ(Util.DegreesToRadians(rotations[index].Z)) * *modelMat; } } } animationTimer = 0.0f; Unsafe.CopyBlock(uniformBuffers_dynamic.mapped, uboDataDynamic_model, (uint)uniformBuffers_dynamic.size); // Flush to make changes visible to the host VkMappedMemoryRange memoryRange = Initializers.mappedMemoryRange(); memoryRange.memory = uniformBuffers_dynamic.memory; memoryRange.size = uniformBuffers_dynamic.size; vkFlushMappedMemoryRanges(device, 1, &memoryRange); }
void setupVertexDescriptions() { // Binding description vertices_bindingDescriptions.Count = 1; vertices_bindingDescriptions[0] = Initializers.vertexInputBindingDescription( VERTEX_BUFFER_BIND_ID, vertexLayout.stride(), VK_VERTEX_INPUT_RATE_VERTEX); // Attribute descriptions vertices_attributeDescriptions.Count = 4; // Location 0 : Position vertices_attributeDescriptions[0] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 0, VK_FORMAT_R32G32B32_SFLOAT, 0); // Location 1 : Texture coordinates vertices_attributeDescriptions[1] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 1, VK_FORMAT_R32G32_SFLOAT, sizeof(float) * 3); // Location 2 : Color vertices_attributeDescriptions[2] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 2, VK_FORMAT_R32G32B32_SFLOAT, sizeof(float) * 5); // Location 3 : Normal vertices_attributeDescriptions[3] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 3, VK_FORMAT_R32G32B32_SFLOAT, sizeof(float) * 8); vertices_inputState = Initializers.pipelineVertexInputStateCreateInfo(); vertices_inputState.vertexBindingDescriptionCount = vertices_bindingDescriptions.Count; vertices_inputState.pVertexBindingDescriptions = (VkVertexInputBindingDescription *)vertices_bindingDescriptions.Data; vertices_inputState.vertexAttributeDescriptionCount = vertices_attributeDescriptions.Count; vertices_inputState.pVertexAttributeDescriptions = (VkVertexInputAttributeDescription *)vertices_attributeDescriptions.Data; }
protected VkCommandBuffer createCommandBuffer(VkCommandBufferLevel level, bool begin) { VkCommandBuffer cmdBuffer; VkCommandBufferAllocateInfo cmdBufAllocateInfo = Initializers.CommandBufferAllocateInfo(cmdPool, level, 1); Util.CheckResult(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, out cmdBuffer)); // If requested, also start the new command buffer if (begin) { VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo(); Util.CheckResult(vkBeginCommandBuffer(cmdBuffer, &cmdBufInfo)); } return(cmdBuffer); }
void setupDescriptorPool() { // Example uses three ubos and one image sampler NativeList <VkDescriptorPoolSize> poolSizes = new NativeList <VkDescriptorPoolSize> { Initializers.descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 4), Initializers.descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 6) }; VkDescriptorPoolCreateInfo descriptorPoolInfo = Initializers.descriptorPoolCreateInfo( poolSizes.Count, (VkDescriptorPoolSize *)poolSizes.Data, 2); Util.CheckResult(vkCreateDescriptorPool(device, &descriptorPoolInfo, null, out descriptorPool)); }
void setupDescriptorSet() { VkDescriptorSetAllocateInfo allocInfo = VkDescriptorSetAllocateInfo.New(); allocInfo.descriptorPool = descriptorPool; allocInfo.pSetLayouts = (VkDescriptorSetLayout *)descriptorSetLayout.Data.ToPointer(); allocInfo.descriptorSetCount = 1; Util.CheckResult(vkAllocateDescriptorSets(Device, &allocInfo, out descriptorSet)); var descriptor = uniformBuffer.descriptor; VkWriteDescriptorSet writeDescriptorSets = Initializers.writeDescriptorSet( descriptorSet, VkDescriptorType.UniformBuffer, 0, &descriptor); vkUpdateDescriptorSets(Device, 1, ref writeDescriptorSets, 0, null); }
void setupDescriptorSet() { var dsl = descriptorSetLayout; VkDescriptorSetAllocateInfo allocInfo = Initializers.descriptorSetAllocateInfo( descriptorPool, &dsl, 1); Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSet)); var descriptor1 = uniformBuffer.descriptor; var descriptor2 = textures_colormap.descriptor; FixedArray2 <VkWriteDescriptorSet> writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>( Initializers.writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &descriptor1), Initializers.writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &descriptor2)); vkUpdateDescriptorSets(device, (uint)(writeDescriptorSets.Count), &writeDescriptorSets.First, 0, null); }
void setupDescriptorSetLayout() { VkDescriptorSetLayoutBinding setLayoutBindings = // Binding 0 : Vertex shader uniform buffer Initializers.descriptorSetLayoutBinding( VkDescriptorType.UniformBuffer, VkShaderStageFlags.Vertex, 0); VkDescriptorSetLayoutCreateInfo descriptorLayout = Initializers.descriptorSetLayoutCreateInfo( &setLayoutBindings, 1); Util.CheckResult(vkCreateDescriptorSetLayout(device, &descriptorLayout, null, out descriptorSetLayout)); var dsl = descriptorSetLayout; VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = Initializers.pipelineLayoutCreateInfo( &dsl, 1); // Define push constant // Example uses six light positions as push constants // 6 * 4 * 4 = 96 bytes // Spec requires a minimum of 128 bytes, bigger values // need to be checked against maxPushConstantsSize // But even at only 128 bytes, lots of stuff can fit // inside push constants VkPushConstantRange pushConstantRange = Initializers.pushConstantRange( VkShaderStageFlags.Vertex, pushConstants.Count * (uint)sizeof(Vector4), 0); // Push constant ranges are part of the pipeline layout pipelineLayoutCreateInfo.pushConstantRangeCount = 1; pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange; Util.CheckResult(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, null, out pipelineLayout)); }
void setupVertexDescriptions() { // Binding description vertices.bindingDescriptions.Count = 1; vertices.bindingDescriptions[0] = Initializers.vertexInputBindingDescription( VERTEX_BUFFER_BIND_ID, vertexLayout.GetStride(), VkVertexInputRate.Vertex); // Attribute descriptions // Describes memory layout and shader positions vertices.attributeDescriptions.Count = 3; // Location 0 : Position vertices.attributeDescriptions[0] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 0, VkFormat.R32g32b32Sfloat, 0); // Location 1 : Normal vertices.attributeDescriptions[1] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 1, VkFormat.R32g32b32Sfloat, sizeof(float) * 3); // Location 2 : Texture coordinates vertices.attributeDescriptions[2] = Initializers.vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 2, VkFormat.R32g32Sfloat, sizeof(float) * 5); vertices.inputState = Initializers.pipelineVertexInputStateCreateInfo(); vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.Count; vertices.inputState.pVertexBindingDescriptions = (VkVertexInputBindingDescription*)vertices.bindingDescriptions.Data; vertices.inputState.vertexAttributeDescriptionCount = vertices.attributeDescriptions.Count; vertices.inputState.pVertexAttributeDescriptions = (VkVertexInputAttributeDescription*)vertices.attributeDescriptions.Data; }
/** * Create a buffer on the device * * @param usageFlags Usage flag bitmask for the buffer (i.e. index, vertex, uniform buffer) * @param memoryPropertyFlags Memory properties for this buffer (i.e. device local, host visible, coherent) * @param size Size of the buffer in byes * @param buffer Pointer to the buffer handle acquired by the function * @param memory Pointer to the memory handle acquired by the function * @param data Pointer to the data that should be copied to the buffer after creation (optional, if not set, no data is copied over) * * @return VK_SUCCESS if buffer handle and memory have been created and (optionally passed) data has been copied */ public VkResult createBuffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, ulong size, VkBuffer *buffer, VkDeviceMemory *memory, void *data = null) { // Create the buffer handle VkBufferCreateInfo bufferCreateInfo = Initializers.bufferCreateInfo(usageFlags, size); bufferCreateInfo.sharingMode = VkSharingMode.Exclusive; Util.CheckResult(vkCreateBuffer(LogicalDevice, &bufferCreateInfo, null, buffer)); // Create the memory backing up the buffer handle VkMemoryRequirements memReqs; VkMemoryAllocateInfo memAlloc = Initializers.memoryAllocateInfo(); vkGetBufferMemoryRequirements(LogicalDevice, *buffer, &memReqs); memAlloc.allocationSize = memReqs.size; // Find a memory type index that fits the properties of the buffer memAlloc.memoryTypeIndex = GetMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags); Util.CheckResult(vkAllocateMemory(LogicalDevice, &memAlloc, null, memory)); // If a pointer to the buffer data has been passed, map the buffer and copy over the data if (data != null) { void *mapped; Util.CheckResult(vkMapMemory(LogicalDevice, *memory, 0, size, 0, &mapped)); Unsafe.CopyBlock(mapped, data, (uint)size); // If host coherency hasn't been requested, do a manual flush to make writes visible if ((memoryPropertyFlags & VkMemoryPropertyFlags.HostCoherent) == 0) { VkMappedMemoryRange mappedRange = Initializers.mappedMemoryRange(); mappedRange.memory = *memory; mappedRange.offset = 0; mappedRange.size = size; vkFlushMappedMemoryRanges(LogicalDevice, 1, &mappedRange); } vkUnmapMemory(LogicalDevice, *memory); } // Attach the memory to the buffer object Util.CheckResult(vkBindBufferMemory(LogicalDevice, *buffer, *memory, 0)); return(VkResult.Success); }
void setupDescriptorSet() { var dsl = descriptorSetLayout; VkDescriptorSetAllocateInfo allocInfo = Initializers.descriptorSetAllocateInfo( descriptorPool, &dsl, 1); Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSet)); var descriptor0 = uniformBuffers_view.descriptor; var descriptor1 = uniformBuffers_dynamic.descriptor; FixedArray2 <VkWriteDescriptorSet> writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>( // Binding 0 : Projection/View matrix uniform buffer Initializers.writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &descriptor0), // Binding 1 : Instance matrix as dynamic uniform buffer Initializers.writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1, &descriptor1)); vkUpdateDescriptorSets(device, writeDescriptorSets.Count, &writeDescriptorSets.First, 0, null); }
void setupDescriptorSetLayout() { FixedArray2 <VkDescriptorSetLayoutBinding> setLayoutBindings = new FixedArray2 <VkDescriptorSetLayoutBinding>( Initializers.descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0), Initializers.descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1)); VkDescriptorSetLayoutCreateInfo descriptorLayout = Initializers.descriptorSetLayoutCreateInfo( &setLayoutBindings.First, setLayoutBindings.Count); Util.CheckResult(vkCreateDescriptorSetLayout(device, &descriptorLayout, null, out descriptorSetLayout)); var dsl = descriptorSetLayout; VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = Initializers.pipelineLayoutCreateInfo( &dsl, 1); Util.CheckResult(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, null, out pipelineLayout)); }
void setupVertexDescriptions() { // Binding description vertices_bindingDescriptions = new NativeList <VkVertexInputBindingDescription> { Initializers.vertexInputBindingDescription(VERTEX_BUFFER_BIND_ID, (uint)sizeof(Vertex), VK_VERTEX_INPUT_RATE_VERTEX), }; // Attribute descriptions vertices_attributeDescriptions = new NativeList <VkVertexInputAttributeDescription> { Initializers.vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 0, VK_FORMAT_R32G32B32_SFLOAT, 0), // Location 0 : Position Initializers.vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 1, VK_FORMAT_R32G32B32_SFLOAT, (uint)sizeof(Vector3)), // Location 1 : Color }; vertices_inputState = Initializers.pipelineVertexInputStateCreateInfo(); vertices_inputState.vertexBindingDescriptionCount = vertices_bindingDescriptions.Count; vertices_inputState.pVertexBindingDescriptions = (VkVertexInputBindingDescription *)vertices_bindingDescriptions.Data; vertices_inputState.vertexAttributeDescriptionCount = vertices_attributeDescriptions.Count; vertices_inputState.pVertexAttributeDescriptions = (VkVertexInputAttributeDescription *)vertices_attributeDescriptions.Data; }