195 std::array<VkDescriptorPoolSize, 2> poolSizes{};
196 poolSizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
197 poolSizes[0].descriptorCount = 1;
198 poolSizes[1].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
199 poolSizes[1].descriptorCount = 1;
201 VkDescriptorPoolCreateInfo poolInfo{};
202 poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
203 poolInfo.poolSizeCount =
static_cast<uint32_t
>(poolSizes.size());
204 poolInfo.pPoolSizes = poolSizes.data();
205 poolInfo.maxSets = 1;
209 VkDescriptorSetAllocateInfo allocInfo{};
210 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
212 allocInfo.descriptorSetCount = 1;
217 VkDescriptorImageInfo imageInfo{};
218 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
222 VkDescriptorBufferInfo bufferInfo{};
224 bufferInfo.offset = 0;
227 std::array<VkWriteDescriptorSet, 2> writes{};
228 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
230 writes[0].dstBinding = 0;
231 writes[0].dstArrayElement = 0;
232 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
233 writes[0].descriptorCount = 1;
234 writes[0].pImageInfo = &imageInfo;
236 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
238 writes[1].dstBinding = 1;
239 writes[1].dstArrayElement = 0;
240 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
241 writes[1].descriptorCount = 1;
242 writes[1].pBufferInfo = &bufferInfo;
244 vkUpdateDescriptorSets(
device,
static_cast<uint32_t
>(writes.size()), writes.data(), 0,
nullptr);
416 VkPipelineShaderStageCreateInfo vertStageInfo{};
417 vertStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
418 vertStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
419 vertStageInfo.module = vertModule;
420 vertStageInfo.pName =
"main";
422 VkPipelineShaderStageCreateInfo fragStageInfo{};
423 fragStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
424 fragStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
425 fragStageInfo.module = fragModule;
426 fragStageInfo.pName =
"main";
428 VkPipelineShaderStageCreateInfo shaderStages[] = { vertStageInfo, fragStageInfo };
430 std::array<VkVertexInputBindingDescription, 2> bindingDescs{};
431 bindingDescs[0].binding = 0;
432 bindingDescs[0].stride =
sizeof(float) * 4;
433 bindingDescs[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
434 bindingDescs[1].binding = 1;
436 bindingDescs[1].inputRate = VK_VERTEX_INPUT_RATE_INSTANCE;
438 std::array<VkVertexInputAttributeDescription, 4> attrDescs{};
440 attrDescs[0].binding = 0;
441 attrDescs[0].location = 0;
442 attrDescs[0].format = VK_FORMAT_R32G32_SFLOAT;
443 attrDescs[0].offset = 0;
445 attrDescs[1].binding = 0;
446 attrDescs[1].location = 1;
447 attrDescs[1].format = VK_FORMAT_R32G32_SFLOAT;
448 attrDescs[1].offset =
sizeof(float) * 2;
450 attrDescs[2].binding = 1;
451 attrDescs[2].location = 2;
452 attrDescs[2].format = VK_FORMAT_R32G32B32A32_SFLOAT;
453 attrDescs[2].offset = 0;
455 attrDescs[3].binding = 1;
456 attrDescs[3].location = 3;
457 attrDescs[3].format = VK_FORMAT_R32G32B32A32_SFLOAT;
458 attrDescs[3].offset =
sizeof(float) * 4;
460 VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
461 vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
462 vertexInputInfo.vertexBindingDescriptionCount =
static_cast<uint32_t
>(bindingDescs.size());
463 vertexInputInfo.pVertexBindingDescriptions = bindingDescs.data();
464 vertexInputInfo.vertexAttributeDescriptionCount =
static_cast<uint32_t
>(attrDescs.size());
465 vertexInputInfo.pVertexAttributeDescriptions = attrDescs.data();
467 VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
468 inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
469 inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
470 inputAssembly.primitiveRestartEnable = VK_FALSE;
472 std::vector<VkDynamicState> dynamicStates = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
473 VkPipelineDynamicStateCreateInfo dynamicState{};
474 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
475 dynamicState.dynamicStateCount =
static_cast<uint32_t
>(dynamicStates.size());
476 dynamicState.pDynamicStates = dynamicStates.data();
478 VkPipelineViewportStateCreateInfo viewportState{};
479 viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
480 viewportState.viewportCount = 1;
481 viewportState.scissorCount = 1;
483 VkPipelineRasterizationStateCreateInfo rasterizer{};
484 rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
485 rasterizer.depthClampEnable = VK_FALSE;
486 rasterizer.rasterizerDiscardEnable = VK_FALSE;
487 rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
488 rasterizer.lineWidth = 1.0f;
489 rasterizer.cullMode = VK_CULL_MODE_NONE;
490 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
491 rasterizer.depthBiasEnable = VK_FALSE;
493 VkPipelineMultisampleStateCreateInfo multisampling{};
494 multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
495 multisampling.sampleShadingEnable = VK_FALSE;
496 multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
498 VkPipelineDepthStencilStateCreateInfo depthStencil{};
499 depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
500 depthStencil.depthTestEnable = VK_FALSE;
501 depthStencil.depthWriteEnable = VK_FALSE;
503 VkPipelineColorBlendAttachmentState colorBlendAttachment{};
504 colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
505 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
506 colorBlendAttachment.blendEnable = VK_TRUE;
507 colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
508 colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
509 colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
510 colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
511 colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
512 colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
514 VkPipelineColorBlendStateCreateInfo colorBlending{};
515 colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
516 colorBlending.logicOpEnable = VK_FALSE;
517 colorBlending.attachmentCount = 1;
518 colorBlending.pAttachments = &colorBlendAttachment;
520 VkPushConstantRange pushConstantRange{};
521 pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
522 pushConstantRange.offset = 0;
523 pushConstantRange.size =
sizeof(float) * 2;
525 VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
526 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
527 pipelineLayoutInfo.setLayoutCount = 1;
529 pipelineLayoutInfo.pushConstantRangeCount = 1;
530 pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
534 VkGraphicsPipelineCreateInfo pipelineInfo{};
535 pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
536 pipelineInfo.stageCount = 2;
537 pipelineInfo.pStages = shaderStages;
538 pipelineInfo.pVertexInputState = &vertexInputInfo;
539 pipelineInfo.pInputAssemblyState = &inputAssembly;
540 pipelineInfo.pViewportState = &viewportState;
541 pipelineInfo.pRasterizationState = &rasterizer;
542 pipelineInfo.pMultisampleState = &multisampling;
543 pipelineInfo.pDepthStencilState = &depthStencil;
544 pipelineInfo.pColorBlendState = &colorBlending;
545 pipelineInfo.pDynamicState = &dynamicState;
548 pipelineInfo.subpass = 0;
549 pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
553 vkDestroyShaderModule(
device, vertModule,
nullptr);
554 vkDestroyShaderModule(
device, fragModule,
nullptr);
574 VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
575 vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
576 vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
577 vertShaderStageInfo.module = vertShaderModule;
578 vertShaderStageInfo.pName =
"main";
580 VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
581 fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
582 fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
584 fragShaderStageInfo.pName =
"main";
586 VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
588 VkVertexInputBindingDescription bindingDescription{};
589 bindingDescription.binding = 0;
590 bindingDescription.stride =
sizeof(float) * 4;
591 bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
593 std::array<VkVertexInputAttributeDescription, 2> attributeDescriptions{};
594 attributeDescriptions[0].binding = 0;
595 attributeDescriptions[0].location = 0;
596 attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
597 attributeDescriptions[0].offset = 0;
598 attributeDescriptions[1].binding = 0;
599 attributeDescriptions[1].location = 1;
600 attributeDescriptions[1].format = VK_FORMAT_R32G32_SFLOAT;
601 attributeDescriptions[1].offset =
sizeof(float) * 2;
603 VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
604 vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
605 vertexInputInfo.vertexBindingDescriptionCount = 1;
606 vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
607 vertexInputInfo.vertexAttributeDescriptionCount =
static_cast<uint32_t
>(attributeDescriptions.size());
608 vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
610 VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
611 inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
612 inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
613 inputAssembly.primitiveRestartEnable = VK_FALSE;
615 std::vector<VkDynamicState> dynamicStates = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
616 VkPipelineDynamicStateCreateInfo dynamicState{};
617 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
618 dynamicState.dynamicStateCount =
static_cast<uint32_t
>(dynamicStates.size());
619 dynamicState.pDynamicStates = dynamicStates.data();
621 VkPipelineViewportStateCreateInfo viewportState{};
622 viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
623 viewportState.viewportCount = 1;
624 viewportState.scissorCount = 1;
626 VkPipelineRasterizationStateCreateInfo rasterizer{};
627 rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
628 rasterizer.depthClampEnable = VK_FALSE;
629 rasterizer.rasterizerDiscardEnable = VK_FALSE;
630 rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
631 rasterizer.lineWidth = 1.0f;
632 rasterizer.cullMode = VK_CULL_MODE_NONE;
633 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
634 rasterizer.depthBiasEnable = VK_FALSE;
636 VkPipelineMultisampleStateCreateInfo multisampling{};
637 multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
638 multisampling.sampleShadingEnable = VK_FALSE;
639 multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
641 VkPipelineDepthStencilStateCreateInfo depthStencil{};
642 depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
643 depthStencil.depthTestEnable = VK_FALSE;
644 depthStencil.depthWriteEnable = VK_FALSE;
646 VkPipelineColorBlendAttachmentState colorBlendAttachment{};
647 colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
648 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
649 colorBlendAttachment.blendEnable = VK_TRUE;
650 colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
651 colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
652 colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
653 colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
654 colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
655 colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
657 VkPipelineColorBlendStateCreateInfo colorBlending{};
658 colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
659 colorBlending.logicOpEnable = VK_FALSE;
660 colorBlending.attachmentCount = 1;
661 colorBlending.pAttachments = &colorBlendAttachment;
663 VkPushConstantRange pushConstantRange{};
664 pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
665 pushConstantRange.offset = 0;
666 pushConstantRange.size =
sizeof(float) * 12;
670 VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
671 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
672 pipelineLayoutInfo.setLayoutCount = 1;
673 pipelineLayoutInfo.pSetLayouts = &layoutToUseForPipeline;
674 pipelineLayoutInfo.pushConstantRangeCount = 1;
675 pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
679 VkGraphicsPipelineCreateInfo pipelineInfo{};
680 pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
681 pipelineInfo.stageCount = 2;
682 pipelineInfo.pStages = shaderStages;
683 pipelineInfo.pVertexInputState = &vertexInputInfo;
684 pipelineInfo.pInputAssemblyState = &inputAssembly;
685 pipelineInfo.pViewportState = &viewportState;
686 pipelineInfo.pRasterizationState = &rasterizer;
687 pipelineInfo.pMultisampleState = &multisampling;
688 pipelineInfo.pDepthStencilState = &depthStencil;
689 pipelineInfo.pColorBlendState = &colorBlending;
690 pipelineInfo.pDynamicState = &dynamicState;
693 pipelineInfo.subpass = 0;
694 pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
698 vkDestroyShaderModule(
device, vertShaderModule,
nullptr);
715 {{0.0f, 0.0f}, {0.0f, 0.0f}},
716 {{1.0f, 0.0f}, {1.0f, 0.0f}},
717 {{1.0f, 1.0f}, {1.0f, 1.0f}},
718 {{0.0f, 1.0f}, {0.0f, 1.0f}}
720 uint16_t indices[] = {0, 1, 2, 0, 2, 3};
722 VkDeviceSize vertexSize =
sizeof(vertices);
723 createBuffer(vertexSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
724 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
729 memcpy(data, vertices, vertexSize);
732 VkDeviceSize indexSize =
sizeof(indices);
733 createBuffer(indexSize, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
734 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
738 memcpy(data, indices, indexSize);
958 VkDeviceSize imageSize =
static_cast<VkDeviceSize
>(width) * height * 4;
965 VkCommandBufferBeginInfo beginInfo{};
966 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
967 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
969 VkImageMemoryBarrier barrier{};
970 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
971 barrier.oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
972 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
973 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
974 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
976 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
977 barrier.subresourceRange.baseMipLevel = 0;
978 barrier.subresourceRange.levelCount = 1;
979 barrier.subresourceRange.baseArrayLayer = 0;
980 barrier.subresourceRange.layerCount = 1;
981 barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
982 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
983 vkCmdPipelineBarrier(
uploadCmdBuffer, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
984 0, 0,
nullptr, 0,
nullptr, 1, &barrier);
986 VkBufferImageCopy region{};
987 region.bufferOffset = 0;
988 region.bufferRowLength = 0;
989 region.bufferImageHeight = 0;
990 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
991 region.imageSubresource.mipLevel = 0;
992 region.imageSubresource.baseArrayLayer = 0;
993 region.imageSubresource.layerCount = 1;
994 region.imageOffset = {0, 0, 0};
995 region.imageExtent = {width, height, 1};
998 barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
999 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1000 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1001 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1002 vkCmdPipelineBarrier(
uploadCmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
1003 0, 0,
nullptr, 0,
nullptr, 1, &barrier);
1008 VkSubmitInfo submitInfo{};
1009 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1010 submitInfo.commandBufferCount = 1;
1016 createImage(surface->w, surface->h, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
1017 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
1020 VkBuffer stagingBuffer;
1021 VkDeviceMemory stagingMemory;
1022 VkDeviceSize imageSize =
static_cast<VkDeviceSize
>(surface->w) * surface->h * 4;
1024 createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1025 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1026 stagingBuffer, stagingMemory);
1030 memcpy(data, surface->pixels, imageSize);
1031 vkUnmapMemory(
device, stagingMemory);
1037 vkDestroyBuffer(
device, stagingBuffer,
nullptr);
1038 vkFreeMemory(
device, stagingMemory,
nullptr);
1094 uint32_t screenWidth, uint32_t screenHeight) {
1102 uint32_t instanceCount =
static_cast<uint32_t
>(
drawQueue.size());
1111 for (uint32_t i = 0; i < instanceCount; ++i) {
1113 dst[i].
posX = cmd.x;
1114 dst[i].
posY = cmd.y;
1115 dst[i].
sizeW = cmd.w;
1116 dst[i].
sizeH = cmd.h;
1117 dst[i].
params[0] = cmd.params.x;
1118 dst[i].
params[1] = cmd.params.y;
1119 dst[i].
params[2] = cmd.params.z;
1120 dst[i].
params[3] = cmd.params.w;
1123 vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
instancedPipeline);
1129 VkDeviceSize bufOffsets[] = {0, 0};
1130 vkCmdBindVertexBuffers(cmdBuffer, 0, 2, buffers, bufOffsets);
1131 vkCmdBindIndexBuffer(cmdBuffer,
quadIndexBuffer, 0, VK_INDEX_TYPE_UINT16);
1134 float screenSize[2] = {(float)screenWidth, (
float)screenHeight};
1136 0,
sizeof(screenSize), screenSize);
1139 vkCmdDrawIndexed(cmdBuffer, 6, instanceCount, 0, 0, 0);
1146 vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
customPipeline);
1155 vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, layoutToUse,
1158 vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, layoutToUse,
1163 VkDeviceSize offsets[] = {0};
1164 vkCmdBindVertexBuffers(cmdBuffer, 0, 1, vertexBuffers, offsets);
1165 vkCmdBindIndexBuffer(cmdBuffer,
quadIndexBuffer, 0, VK_INDEX_TYPE_UINT16);
1168 struct SpritePushConstants {
1179 (float)screenWidth, (
float)screenHeight,
1180 cmd.x, cmd.y, cmd.w, cmd.h,
1182 {cmd.params.x, cmd.params.y, cmd.params.z, cmd.params.w}
1185 vkCmdPushConstants(cmdBuffer, layoutToUse, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
1186 0,
sizeof(SpritePushConstants), &pc);
1188 vkCmdDrawIndexed(cmdBuffer, 6, 1, 0, 0, 0);
1213 throw mx::Exception(
"VKSprite::createDescriptorSet called before setDescriptorSetLayout");
1220 VkDescriptorSetAllocateInfo allocInfo{};
1221 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
1223 allocInfo.descriptorSetCount = 1;
1226 VkDescriptorSet descSet;
1229 VkDescriptorImageInfo imageInfo{};
1230 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1231 imageInfo.imageView = imageView;
1234 VkWriteDescriptorSet descriptorWrite{};
1235 descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1236 descriptorWrite.dstSet = descSet;
1237 descriptorWrite.dstBinding = 0;
1238 descriptorWrite.dstArrayElement = 0;
1239 descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1240 descriptorWrite.descriptorCount = 1;
1241 descriptorWrite.pImageInfo = &imageInfo;
1243 vkUpdateDescriptorSets(
device, 1, &descriptorWrite, 0,
nullptr);
1287 VkImageMemoryBarrier barrier{};
1288 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1289 barrier.oldLayout = oldLayout;
1290 barrier.newLayout = newLayout;
1291 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1292 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1293 barrier.image = image;
1294 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1295 barrier.subresourceRange.baseMipLevel = 0;
1296 barrier.subresourceRange.levelCount = 1;
1297 barrier.subresourceRange.baseArrayLayer = 0;
1298 barrier.subresourceRange.layerCount = 1;
1300 VkPipelineStageFlags sourceStage;
1301 VkPipelineStageFlags destinationStage;
1303 if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
1304 barrier.srcAccessMask = 0;
1305 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1306 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1307 destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1308 }
else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
1309 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1310 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1311 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1312 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
1313 }
else if (oldLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
1314 barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
1315 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1316 sourceStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
1317 destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1319 throw std::invalid_argument(
"unsupported layout transition!");
1322 vkCmdPipelineBarrier(commandBuffer, sourceStage, destinationStage, 0, 0,
nullptr, 0,
nullptr, 1, &barrier);
1379 VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
1380 VkImage& image, VkDeviceMemory& imageMemory) {
1381 VkImageCreateInfo imageInfo{};
1382 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1383 imageInfo.imageType = VK_IMAGE_TYPE_2D;
1384 imageInfo.extent.width = width;
1385 imageInfo.extent.height = height;
1386 imageInfo.extent.depth = 1;
1387 imageInfo.mipLevels = 1;
1388 imageInfo.arrayLayers = 1;
1389 imageInfo.format = format;
1390 imageInfo.tiling = tiling;
1391 imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1392 imageInfo.usage = usage;
1393 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1394 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
1398 VkMemoryRequirements memRequirements;
1399 vkGetImageMemoryRequirements(
device, image, &memRequirements);
1401 VkMemoryAllocateInfo allocInfo{};
1402 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1403 allocInfo.allocationSize = memRequirements.size;
1404 allocInfo.memoryTypeIndex =
findMemoryType(memRequirements.memoryTypeBits, properties);