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])); } }
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])); } }
// 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) { 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])); } }
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); }
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); // Update light positions // w component = light radius scale const float r = 7.5f; float sin_t = (float)Math.Sin(Util.DegreesToRadians(timer * 360)); float cos_t = (float)Math.Cos(Util.DegreesToRadians(timer * 360)); const float y = -4.0f; pushConstants[0] = new Vector4(r * 1.1f * sin_t, y, r * 1.1f * cos_t, 1.0f); pushConstants[1] = new Vector4(-r * sin_t, y, -r * cos_t, 1.0f); pushConstants[2] = new Vector4(r * 0.85f * sin_t, y, -sin_t * 2.5f, 1.5f); pushConstants[3] = new Vector4(0.0f, y, r * 1.25f * cos_t, 1.5f); pushConstants[4] = new Vector4(r * 2.25f * cos_t, y, 0.0f, 1.25f); pushConstants[5] = new Vector4(r * 2.5f * (float)Math.Cos(Util.DegreesToRadians(timer * 360)), y, r * 2.5f * sin_t, 1.25f); // Submit via push constant (rather than a UBO) vkCmdPushConstants( drawCmdBuffers[i], pipelineLayout, VkShaderStageFlags.Vertex, 0, pushConstants.Count * (uint)sizeof(Vector4), pushConstants.Data.ToPointer()); vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_solid); vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSet, 0, null); ulong offsets = 0; vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref models_scene.vertices.buffer, &offsets); vkCmdBindIndexBuffer(drawCmdBuffers[i], models_scene.indices.buffer, 0, VkIndexType.Uint32); vkCmdDrawIndexed(drawCmdBuffers[i], models_scene.indexCount, 1, 0, 0, 0); vkCmdEndRenderPass(drawCmdBuffers[i]); Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i])); } }