67 VK_Sprite(VkDevice device, VkPhysicalDevice physicalDevice, VkQueue graphicsQueue,
68 VkCommandPool commandPool);
83 void loadSprite(
const std::string &pngPath,
const std::string &fragmentShaderPath =
"");
90 void loadSprite(SDL_Surface *
surface,
const std::string &fragmentShaderPath =
"");
99 void createEmptySprite(
int width,
int height,
const std::string &vertexShaderPath =
"",
const std::string &fragmentShaderPath =
"");
115 void drawSprite(
int x,
int y,
float scaleX,
float scaleY);
125 void drawSprite(
int x,
int y,
float scaleX,
float scaleY,
float rotation);
146 void updateTexture(
const void *pixels,
int width,
int height,
int pitch = 0);
153 bool updateTextureCuda(
const cv::cuda::GpuMat &rgba, cv::cuda::Stream &stream);
160 void setShaderParams(
float p1 = 0.0f,
float p2 = 0.0f,
float p3 = 0.0f,
float p4 = 0.0f);
178 void renderSprites(VkCommandBuffer cmdBuffer, VkPipelineLayout pipelineLayout,
179 uint32_t screenWidth, uint32_t screenHeight);
237 uint32_t localSizeY, uint32_t localSizeZ = 1);
241 return computePipeline != VK_NULL_HANDLE;
246 VkImageView outputView, uint32_t width,
270 const std::string &instanceVertShaderPath,
271 const std::string &instanceFragShaderPath);
289 void setMouseState(
float mx,
float my,
float pressed,
float reserved = 0.0f);
292 void setUniform0(
float x,
float y,
float z,
float w);
294 void setUniform1(
float x,
float y,
float z,
float w);
296 void setUniform2(
float x,
float y,
float z,
float w);
298 void setUniform3(
float x,
float y,
float z,
float w);
311 void setAudioBands(
float low,
float mid,
float high,
float reserved = 0.0f);
380 [[nodiscard]]
bool updateHistoryTextureCuda(
const cv::cuda::GpuMat &rgba,
381 cv::cuda::Stream &stream);
441 struct SpriteVertex {
446 struct SpriteDrawCmd {
452 VkDevice device = VK_NULL_HANDLE;
453 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
454 VkQueue graphicsQueue = VK_NULL_HANDLE;
455 VkCommandPool commandPool = VK_NULL_HANDLE;
456 VkPipelineCache pipelineCache = VK_NULL_HANDLE;
457 VkImage spriteImage = VK_NULL_HANDLE;
458 VkDeviceMemory spriteImageMemory = VK_NULL_HANDLE;
459 VkImageView spriteImageView = VK_NULL_HANDLE;
461 int spriteHeight = 0;
462 bool spriteLoaded =
false;
463 bool externalTexture =
false;
464 VkShaderModule fragmentShaderModule = VK_NULL_HANDLE;
465 VkShaderModule computeShaderModule = VK_NULL_HANDLE;
466 VkPipeline computePipeline = VK_NULL_HANDLE;
467 VkPipelineLayout computePipelineLayout = VK_NULL_HANDLE;
468 VkImageView computeOutputImageView = VK_NULL_HANDLE;
469 uint32_t computeLocalSizeX = 1;
470 uint32_t computeLocalSizeY = 1;
471 void createComputePipeline();
472 void destroyComputePipeline();
473 bool hasCustomShader =
false;
474 glm::vec4 shaderParams = glm::vec4(0.0f);
475 bool effectsEnabled =
true;
476 std::vector<SpriteDrawCmd> drawQueue;
478 VkPipeline customPipeline = VK_NULL_HANDLE;
479 VkPipelineLayout customPipelineLayout = VK_NULL_HANDLE;
480 VkRenderPass renderPass = VK_NULL_HANDLE;
481 VkFormat colorAttachmentFormat = VK_FORMAT_UNDEFINED;
482 VkFormat depthAttachmentFormat = VK_FORMAT_UNDEFINED;
483 std::string vertexShaderPath;
484 std::string fragmentShaderPath;
485 void createCustomPipeline();
487 VkBuffer quadVertexBuffer = VK_NULL_HANDLE;
488 VkDeviceMemory quadVertexBufferMemory = VK_NULL_HANDLE;
489 VkBuffer quadIndexBuffer = VK_NULL_HANDLE;
490 VkDeviceMemory quadIndexBufferMemory = VK_NULL_HANDLE;
491 bool quadBufferCreated =
false;
492 void createQuadBuffer();
494 VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
495 std::vector<VkDescriptorPool> descriptorPools{};
496 VkDescriptorPool descriptorSetPool = VK_NULL_HANDLE;
497 uint32_t nextDescriptorPoolSets = 16;
498 VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE;
499 VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
500 std::unordered_map<VkImageView, VkDescriptorSet> externalDescriptorSets{};
501 VkFilter textureFilter = VK_FILTER_LINEAR;
502 void createDescriptorPool();
503 void destroyDescriptorPools();
504 void destroyTextureDescriptorPools();
505 VkDescriptorSet createDescriptorSet(VkImageView imageView);
506 void destroySpriteResources();
508 VkBuffer persistentStagingBuffer = VK_NULL_HANDLE;
509 VkDeviceMemory persistentStagingMemory = VK_NULL_HANDLE;
510 void *persistentStagingMapped =
nullptr;
511 VkDeviceSize persistentStagingSize = 0;
512 VkFence uploadFence = VK_NULL_HANDLE;
513 VkCommandBuffer uploadCmdBuffer = VK_NULL_HANDLE;
514 bool stagingResourcesCreated =
false;
515 [[nodiscard]] VkDeviceSize stagingAllocationSize(VkDeviceSize requiredSize)
const;
516 void createStagingResources(VkDeviceSize size);
517 void destroyStagingResources();
518 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
519 VkMemoryPropertyFlags properties, VkBuffer &buffer,
520 VkDeviceMemory &bufferMemory);
521 uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
522 void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height,
523 uint32_t baseArrayLayer = 0, uint32_t layerCount = 1);
524 VkCommandBuffer beginSingleTimeCommands();
525 void endSingleTimeCommands(VkCommandBuffer commandBuffer);
526 void transitionImageLayout(VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout,
527 uint32_t baseArrayLayer = 0, uint32_t layerCount = 1);
528 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling,
529 VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
530 VkImage &image, VkDeviceMemory &imageMemory,
531 uint32_t arrayLayers = 1,
532 VkImageType imageType = VK_IMAGE_TYPE_2D);
533 VkImageView createImageView(VkImage image, VkFormat format,
534 VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D,
535 uint32_t layerCount = 1);
536 void createSampler();
537 SDL_Surface *convertToRGBA(SDL_Surface *surface);
538 void createSpriteTexture(SDL_Surface *surface);
539 void updateSpriteTexture(
const void *pixels, uint32_t width, uint32_t height);
541 void destroyCudaInterop();
542 void destroyCudaHistoryInterop();
543 bool ensureCudaInterop();
544 bool ensureCudaHistoryInterop();
545 bool transitionCudaImageForWrite();
546 bool transitionCudaImageForShaderRead();
547 void transitionCudaHistoryLayer(VkImageLayout oldLayout,
548 VkImageLayout newLayout,
549 VkAccessFlags sourceAccess,
550 VkAccessFlags destinationAccess,
551 VkPipelineStageFlags sourceStage,
552 VkPipelineStageFlags destinationStage);
553 bool updateTextureCudaHost(
const void *pixels, uint32_t width, uint32_t height, uint32_t pitch);
554 void recordCudaReadyBarrier(VkCommandBuffer cmdBuffer);
555 void createCudaExportableImage(uint32_t width, uint32_t height,
556 uint32_t arrayLayers, VkImage &image,
557 VkDeviceMemory &imageMemory,
558 VkDeviceSize &exportMemorySize);
559 VkDeviceSize cudaExportMemorySize = 0;
560 cudaExternalMemory_t cudaExternalMemory =
nullptr;
561 cudaMipmappedArray_t cudaMipmappedArray =
nullptr;
562 cudaArray_t cudaArray =
nullptr;
563 bool cudaInteropEnabled =
false;
564 bool cudaInteropUnavailableLogged =
false;
565 bool cudaUploadLogged =
false;
566 bool cudaWriteTransitionLogged =
false;
567 bool cudaSampleBarrierLogged =
false;
568 bool cudaImageNeedsShaderBarrier =
false;
569 VkImageLayout cudaImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
570 VkDeviceSize cudaHistoryExportMemorySize = 0;
571 cudaExternalMemory_t cudaHistoryExternalMemory =
nullptr;
572 cudaMipmappedArray_t cudaHistoryMipmappedArray =
nullptr;
573 cudaArray_t cudaHistoryArray =
nullptr;
574 bool cudaHistoryInteropEnabled =
false;
575 bool cudaHistoryInteropUnavailableLogged =
false;
576 bool cudaHistoryUploadLogged =
false;
578 std::vector<char> readShaderFile(
const std::string &filename);
580 struct SpriteExtendedUBO {
587 glm::vec4 audio_bands;
588 glm::vec4 audio_history;
590 bool extendedUBOEnabled =
false;
591 SpriteExtendedUBO extendedUBOData{};
592 VkBuffer extendedUBOBuffer = VK_NULL_HANDLE;
593 VkDeviceMemory extendedUBOMemory = VK_NULL_HANDLE;
594 void *extendedUBOMapped =
nullptr;
595 VkDescriptorSetLayout extendedDescriptorSetLayout = VK_NULL_HANDLE;
596 VkDescriptorPool extendedDescriptorPool = VK_NULL_HANDLE;
597 VkDescriptorSet extendedDescriptorSet = VK_NULL_HANDLE;
598 bool ownExtendedDescriptorSetLayout =
false;
599 void createExtendedUBO();
600 void updateExtendedUBO();
601 void createExtendedDescriptorSetLayout();
602 void createExtendedDescriptorSet();
603 void destroyExtendedUBO();
605 bool historyTextureEnabled =
false;
606 bool historyTextureShared =
false;
607 VkImage historyImage = VK_NULL_HANDLE;
608 VkDeviceMemory historyImageMemory = VK_NULL_HANDLE;
609 VkImageView historyImageView = VK_NULL_HANDLE;
610 uint32_t historyWidth = 0;
611 uint32_t historyHeight = 0;
612 uint32_t historyLayers = 0;
613 uint32_t historyHead = 0;
614 void destroyHistoryTexture();
616 bool spectrumTextureEnabled =
false;
617 VkImage spectrumImage = VK_NULL_HANDLE;
618 VkDeviceMemory spectrumImageMemory = VK_NULL_HANDLE;
619 VkImageView spectrumImageView = VK_NULL_HANDLE;
620 uint32_t spectrumBins = 0;
621 void destroySpectrumTexture();
623 bool spectrumHistoryTextureEnabled =
false;
624 VkImage spectrumHistoryImage = VK_NULL_HANDLE;
625 VkDeviceMemory spectrumHistoryImageMemory = VK_NULL_HANDLE;
626 VkImageView spectrumHistoryImageView = VK_NULL_HANDLE;
627 uint32_t spectrumHistoryBins = 0;
628 uint32_t spectrumHistoryLayers = 0;
629 uint32_t spectrumHistoryHead = 0;
630 uint32_t spectrumHistoryWriteIndex = 0;
631 void destroySpectrumHistoryTexture();
632 void recreateExtendedDescriptorLayout();
634 struct SpriteInstanceData {
635 float posX, posY, sizeW, sizeH;
638 VkBuffer instanceBuffer = VK_NULL_HANDLE;
639 VkDeviceMemory instanceBufferMemory = VK_NULL_HANDLE;
640 void *instanceBufferMapped =
nullptr;
641 uint32_t instanceBufferCapacity = 0;
642 bool instancingEnabled =
false;
643 VkPipeline instancedPipeline = VK_NULL_HANDLE;
644 VkPipelineLayout instancedPipelineLayout = VK_NULL_HANDLE;
645 std::string instanceVertPath;
646 std::string instanceFragPath;
647 void createInstancedPipeline(
const std::string &vertPath,
const std::string &fragPath);
648 void ensureInstanceBuffer(uint32_t count);
649 void destroyInstanceResources();