* Python binding wrapper for returning the content of a texture as a numpy array. */
| 754 | * Python binding wrapper for returning the content of a texture as a numpy array. |
| 755 | */ |
| 756 | inline pybind11::ndarray<pybind11::numpy> texture_to_numpy(const Texture& self, uint32_t mip_level, uint32_t array_slice) |
| 757 | { |
| 758 | FALCOR_CHECK( |
| 759 | mip_level < self.getMipCount(), "'mip_level' ({}) is out of bounds. Only {} level(s) available.", mip_level, self.getMipCount() |
| 760 | ); |
| 761 | FALCOR_CHECK( |
| 762 | array_slice < self.getArraySize(), |
| 763 | "'array_slice' ({}) is out of bounds. Only {} slice(s) available.", |
| 764 | array_slice, |
| 765 | self.getArraySize() |
| 766 | ); |
| 767 | |
| 768 | // Get image dimensions. |
| 769 | uint32_t width = self.getWidth(mip_level); |
| 770 | uint32_t height = self.getHeight(mip_level); |
| 771 | uint32_t depth = self.getDepth(mip_level); |
| 772 | |
| 773 | uint32_t subresource = self.getSubresourceIndex(array_slice, mip_level); |
| 774 | Texture::SubresourceLayout layout = self.getSubresourceLayout(subresource); |
| 775 | |
| 776 | size_t subresourceSize = layout.getTotalByteSize(); |
| 777 | void* cpuData = new uint8_t[subresourceSize]; |
| 778 | self.getSubresourceBlob(subresource, cpuData, subresourceSize); |
| 779 | |
| 780 | pybind11::capsule owner(cpuData, [](void* p) noexcept { delete[] reinterpret_cast<uint8_t*>(p); }); |
| 781 | |
| 782 | if (auto dtype = resourceFormatToDtype(self.getFormat())) |
| 783 | { |
| 784 | uint32_t channelCount = getFormatChannelCount(self.getFormat()); |
| 785 | std::vector<pybind11::size_t> shape; |
| 786 | if (depth > 1) |
| 787 | shape.push_back(depth); |
| 788 | if (height > 1) |
| 789 | shape.push_back(height); |
| 790 | shape.push_back(width); |
| 791 | if (channelCount > 1) |
| 792 | shape.push_back(channelCount); |
| 793 | return pybind11::ndarray<pybind11::numpy>( |
| 794 | cpuData, shape.size(), shape.data(), owner, nullptr, *dtype, pybind11::device::cpu::value |
| 795 | ); |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | pybind11::size_t shape[1] = {subresourceSize}; |
| 800 | return pybind11::ndarray<pybind11::numpy>( |
| 801 | cpuData, 1, shape, owner, nullptr, pybind11::dtype<uint8_t>(), pybind11::device::cpu::value |
| 802 | ); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | inline void texture_from_numpy(Texture& self, pybind11::ndarray<pybind11::numpy> data, uint32_t mip_level, uint32_t array_slice) |
| 807 | { |
nothing calls this directly
no test coverage detected