| 734 | namespace donut::engine |
| 735 | { |
| 736 | bool SaveTextureToFile( |
| 737 | nvrhi::IDevice* device, |
| 738 | CommonRenderPasses* pPasses, |
| 739 | nvrhi::ITexture* texture, |
| 740 | nvrhi::ResourceStates textureState, |
| 741 | const char* fileName, |
| 742 | bool saveAlphaChannel) |
| 743 | { |
| 744 | if (!fileName) |
| 745 | return false; |
| 746 | |
| 747 | // Find the file's extension |
| 748 | char const* ext = strrchr(fileName, '.'); |
| 749 | |
| 750 | if (!ext) |
| 751 | return false; // No extension fond in the file name |
| 752 | |
| 753 | // Determine the image format from the extension |
| 754 | enum { BMP, PNG, JPG, TGA } destFormat; |
| 755 | if (strcasecmp(ext, ".bmp") == 0) |
| 756 | destFormat = BMP; |
| 757 | else if (strcasecmp(ext, ".png") == 0) |
| 758 | destFormat = PNG; |
| 759 | else if (strcasecmp(ext, ".jpg") == 0 || strcasecmp(ext, ".jpeg") == 0) |
| 760 | destFormat = JPG; |
| 761 | else if (strcasecmp(ext, ".tga") == 0) |
| 762 | destFormat = TGA; |
| 763 | else |
| 764 | return false; // Unknown file type |
| 765 | |
| 766 | if (destFormat == JPG) |
| 767 | saveAlphaChannel = false; |
| 768 | |
| 769 | nvrhi::TextureDesc desc = texture->getDesc(); |
| 770 | nvrhi::TextureHandle tempTexture; |
| 771 | nvrhi::FramebufferHandle tempFramebuffer; |
| 772 | |
| 773 | nvrhi::CommandListHandle commandList = device->createCommandList(); |
| 774 | commandList->open(); |
| 775 | |
| 776 | if (textureState != nvrhi::ResourceStates::Unknown) |
| 777 | { |
| 778 | commandList->beginTrackingTextureState(texture, nvrhi::TextureSubresourceSet(0, 1, 0, 1), textureState); |
| 779 | } |
| 780 | |
| 781 | // If the source texture format is not RGBA8, create a temporary texture and blit into it to convert |
| 782 | switch (desc.format) |
| 783 | { |
| 784 | case nvrhi::Format::RGBA8_UNORM: |
| 785 | case nvrhi::Format::SRGBA8_UNORM: |
| 786 | tempTexture = texture; |
| 787 | break; |
| 788 | default: |
| 789 | desc.format = nvrhi::Format::SRGBA8_UNORM; |
| 790 | desc.isRenderTarget = true; |
| 791 | desc.initialState = nvrhi::ResourceStates::RenderTarget; |
| 792 | desc.keepInitialState = true; |
| 793 |
nothing calls this directly
no test coverage detected