|
| 1 | +//===- FormatConversion.h - Bridge between DataFormat and Format -----------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// Transitional helpers for converting between the legacy DataFormat + Channels |
| 10 | +// description system and the unified Format enum. This file should be deleted |
| 11 | +// once the pipeline is fully migrated to use Format directly. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#ifndef OFFLOADTEST_API_FORMATCONVERSION_H |
| 16 | +#define OFFLOADTEST_API_FORMATCONVERSION_H |
| 17 | + |
| 18 | +#include "API/Resources.h" |
| 19 | +#include "API/Texture.h" |
| 20 | +#include "Support/Pipeline.h" |
| 21 | + |
| 22 | +#include "llvm/Support/Error.h" |
| 23 | + |
| 24 | +namespace offloadtest { |
| 25 | + |
| 26 | +// Bridge for code that still describes textures as DataFormat + Channels (e.g. |
| 27 | +// render targets bound via CPUBuffer). Once the pipeline is refactored to use |
| 28 | +// Format directly, this function can be removed. |
| 29 | +inline llvm::Expected<Format> toFormat(DataFormat Format, int Channels) { |
| 30 | + switch (Format) { |
| 31 | + case DataFormat::Int16: |
| 32 | + switch (Channels) { |
| 33 | + case 1: |
| 34 | + return Format::R16Sint; |
| 35 | + case 2: |
| 36 | + return Format::RG16Sint; |
| 37 | + case 4: |
| 38 | + return Format::RGBA16Sint; |
| 39 | + } |
| 40 | + break; |
| 41 | + case DataFormat::UInt16: |
| 42 | + switch (Channels) { |
| 43 | + case 1: |
| 44 | + return Format::R16Uint; |
| 45 | + case 2: |
| 46 | + return Format::RG16Uint; |
| 47 | + case 4: |
| 48 | + return Format::RGBA16Uint; |
| 49 | + } |
| 50 | + break; |
| 51 | + case DataFormat::Int32: |
| 52 | + switch (Channels) { |
| 53 | + case 1: |
| 54 | + return Format::R32Sint; |
| 55 | + case 2: |
| 56 | + return Format::RG32Sint; |
| 57 | + case 4: |
| 58 | + return Format::RGBA32Sint; |
| 59 | + } |
| 60 | + break; |
| 61 | + case DataFormat::UInt32: |
| 62 | + switch (Channels) { |
| 63 | + case 1: |
| 64 | + return Format::R32Uint; |
| 65 | + case 2: |
| 66 | + return Format::RG32Uint; |
| 67 | + case 4: |
| 68 | + return Format::RGBA32Uint; |
| 69 | + } |
| 70 | + break; |
| 71 | + case DataFormat::Float32: |
| 72 | + switch (Channels) { |
| 73 | + case 1: |
| 74 | + return Format::R32Float; |
| 75 | + case 2: |
| 76 | + return Format::RG32Float; |
| 77 | + case 4: |
| 78 | + return Format::RGBA32Float; |
| 79 | + } |
| 80 | + break; |
| 81 | + case DataFormat::Depth32: |
| 82 | + // D32FloatS8Uint is not expressible as DataFormat + Channels because the |
| 83 | + // stencil component is uint8, not a second Depth32 channel. Once the |
| 84 | + // pipeline uses Format directly, this limitation goes away. |
| 85 | + if (Channels == 1) |
| 86 | + return Format::D32Float; |
| 87 | + break; |
| 88 | + // No Format mapping for these DataFormats. |
| 89 | + case DataFormat::Hex8: |
| 90 | + case DataFormat::Hex16: |
| 91 | + case DataFormat::Hex32: |
| 92 | + case DataFormat::Hex64: |
| 93 | + case DataFormat::UInt64: |
| 94 | + case DataFormat::Int64: |
| 95 | + case DataFormat::Float16: |
| 96 | + case DataFormat::Float64: |
| 97 | + case DataFormat::Bool: |
| 98 | + return llvm::createStringError(std::errc::invalid_argument, |
| 99 | + "DataFormat %d has no Format equivalent.", |
| 100 | + static_cast<int>(Format)); |
| 101 | + } |
| 102 | + return llvm::createStringError(std::errc::invalid_argument, |
| 103 | + "No Format for DataFormat %d with %d " |
| 104 | + "channel(s).", |
| 105 | + static_cast<int>(Format), Channels); |
| 106 | +} |
| 107 | + |
| 108 | +// Validates that a TextureCreateDesc is consistent with the CPUBuffer it was |
| 109 | +// derived from. Call this after building a TextureCreateDesc from a CPUBuffer |
| 110 | +// to catch mismatches between the two description systems. |
| 111 | +inline llvm::Error |
| 112 | +validateTextureDescMatchesCPUBuffer(const TextureCreateDesc &Desc, |
| 113 | + const CPUBuffer &Buf) { |
| 114 | + auto ExpectedFmt = toFormat(Buf.Format, Buf.Channels); |
| 115 | + if (!ExpectedFmt) |
| 116 | + return ExpectedFmt.takeError(); |
| 117 | + if (Desc.Format != *ExpectedFmt) |
| 118 | + return llvm::createStringError( |
| 119 | + std::errc::invalid_argument, |
| 120 | + "TextureCreateDesc format '%s' does not match CPUBuffer format " |
| 121 | + "(DataFormat %d, %d channels -> '%s').", |
| 122 | + getFormatName(Desc.Format).data(), static_cast<int>(Buf.Format), |
| 123 | + Buf.Channels, getFormatName(*ExpectedFmt).data()); |
| 124 | + if (Desc.Width != static_cast<uint32_t>(Buf.OutputProps.Width)) |
| 125 | + return llvm::createStringError( |
| 126 | + std::errc::invalid_argument, |
| 127 | + "TextureCreateDesc width %u does not match CPUBuffer width %d.", |
| 128 | + Desc.Width, Buf.OutputProps.Width); |
| 129 | + if (Desc.Height != static_cast<uint32_t>(Buf.OutputProps.Height)) |
| 130 | + return llvm::createStringError( |
| 131 | + std::errc::invalid_argument, |
| 132 | + "TextureCreateDesc height %u does not match CPUBuffer height %d.", |
| 133 | + Desc.Height, Buf.OutputProps.Height); |
| 134 | + if (Desc.MipLevels != static_cast<uint32_t>(Buf.OutputProps.MipLevels)) |
| 135 | + return llvm::createStringError( |
| 136 | + std::errc::invalid_argument, |
| 137 | + "TextureCreateDesc mip levels %u does not match CPUBuffer mip " |
| 138 | + "levels %d.", |
| 139 | + Desc.MipLevels, Buf.OutputProps.MipLevels); |
| 140 | + uint32_t TexelSize = getFormatSize(Desc.Format); |
| 141 | + if (Buf.Stride > 0 && static_cast<uint32_t>(Buf.Stride) != TexelSize) |
| 142 | + return llvm::createStringError( |
| 143 | + std::errc::invalid_argument, |
| 144 | + "CPUBuffer stride %d does not match texture format element size %u.", |
| 145 | + Buf.Stride, TexelSize); |
| 146 | + uint64_t ExpectedSize = |
| 147 | + static_cast<uint64_t>(Desc.Width) * Desc.Height * TexelSize; |
| 148 | + if (static_cast<uint64_t>(Buf.size()) != ExpectedSize) |
| 149 | + return llvm::createStringError( |
| 150 | + std::errc::invalid_argument, |
| 151 | + "CPUBuffer size %u does not match expected size %llu " |
| 152 | + "(width %u * height %u * element size %u).", |
| 153 | + Buf.size(), ExpectedSize, Desc.Width, Desc.Height, TexelSize); |
| 154 | + return llvm::Error::success(); |
| 155 | +} |
| 156 | + |
| 157 | +} // namespace offloadtest |
| 158 | + |
| 159 | +#endif // OFFLOADTEST_API_FORMATCONVERSION_H |
0 commit comments