// 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 == 0) { 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(0.0f, 0.0f, 0.0f, 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); 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])); } }
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 = (VkClearValue *)Unsafe.AsPointer(ref clearValues); 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, pipelines_solid); ulong offsets = 0; vkCmdBindVertexBuffers(DrawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref vertexBuffer.buffer, &offsets); vkCmdBindIndexBuffer(DrawCmdBuffers[i], indexBuffer.buffer, 0, VkIndexType.Uint32); vkCmdDrawIndexed(DrawCmdBuffers[i], indexCount, 1, 0, 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, 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); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, ref descriptorSet, 0, null); ulong offsets = 0; vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref models_cube.vertices.buffer, &offsets); vkCmdBindIndexBuffer(drawCmdBuffers[i], models_cube.indices.buffer, 0, VK_INDEX_TYPE_UINT32); // Left viewport.width = (float)width / 3.0f; vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_phong); vkCmdDrawIndexed(drawCmdBuffers[i], models_cube.indexCount, 1, 0, 0, 0); // Center viewport.x = (float)width / 3.0f; vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_toon); vkCmdDrawIndexed(drawCmdBuffers[i], models_cube.indexCount, 1, 0, 0, 0); // Right viewport.x = (float)width / 3.0f + (float)width / 3.0f; vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_textured); vkCmdDrawIndexed(drawCmdBuffers[i], models_cube.indexCount, 1, 0, 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); // 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])); } }