| 1 | #include <nn/gfx/detail/gfx_Texture-api.nvn.8.h> |
| 2 | |
| 3 | #include <nn/gfx/detail/gfx_Device-api.nvn.8.h> |
| 4 | #include <nn/gfx/detail/gfx_MemoryPool-api.nvn.8.h> |
| 5 | #include <nn/gfx/gfx_TextureInfo.h> |
| 6 | #include <nn/util/util_BitUtil.h> |
| 7 | |
| 8 | #include <algorithm> |
| 9 | |
| 10 | #include "gfx_CommonHelper.h" |
| 11 | #include "gfx_NvnHelper.h" |
| 12 | |
| 13 | namespace nn::gfx::detail { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | void SetupTextureBuilder(NVNtextureBuilder* pBuilder, NVNdevice* pDevice, const TextureInfo& info) { |
| 18 | ImageStorageDimension imageStorageDimension = info.GetImageStorageDimension(); |
| 19 | int arrayLength = info.GetArrayLength(); |
| 20 | NVNtextureTarget target = Nvn::GetImageTarget( |
| 21 | GetImageDimension(imageStorageDimension, arrayLength > 1, info.GetMultisampleCount() > 1)); |
| 22 | |
| 23 | nvnTextureBuilderSetDefaults(builder: pBuilder); |
| 24 | nvnTextureBuilderSetDevice(builder: pBuilder, device: pDevice); |
| 25 | nvnTextureBuilderSetWidth(builder: pBuilder, width: info.GetWidth()); |
| 26 | nvnTextureBuilderSetHeight( |
| 27 | builder: pBuilder, height: (target == NVN_TEXTURE_TARGET_1D_ARRAY) ? arrayLength : info.GetHeight()); |
| 28 | nvnTextureBuilderSetDepth(builder: pBuilder, depth: (target == NVN_TEXTURE_TARGET_2D_ARRAY || |
| 29 | target == NVN_TEXTURE_TARGET_2D_MULTISAMPLE_ARRAY) ? |
| 30 | arrayLength : |
| 31 | info.GetDepth()); |
| 32 | nvnTextureBuilderSetLevels(builder: pBuilder, numLevels: info.GetMipCount()); |
| 33 | |
| 34 | if (info.GetMultisampleCount() > 1) { |
| 35 | nvnTextureBuilderSetSamples(builder: pBuilder, samples: info.GetMultisampleCount()); |
| 36 | } |
| 37 | |
| 38 | nvnTextureBuilderSetTarget(builder: pBuilder, target); |
| 39 | nvnTextureBuilderSetFormat(builder: pBuilder, format: Nvn::GetImageFormat(info.GetImageFormat())); |
| 40 | |
| 41 | int flags = NVN_TEXTURE_FLAGS_IMAGE; |
| 42 | flags |= (info.GetGpuAccessFlags() & (GpuAccess_DepthStencil | GpuAccess_ColorBuffer)) ? |
| 43 | NVN_TEXTURE_FLAGS_COMPRESSIBLE : |
| 44 | 0; |
| 45 | |
| 46 | flags |= (info.GetGpuAccessFlags() & GpuAccess_ScanBuffer) ? NVN_TEXTURE_FLAGS_DISPLAY : 0; |
| 47 | |
| 48 | flags |= (info.ToData()->flags.GetBit(p: info.ToData()->Flag_SparseResidency)) ? |
| 49 | NVN_TEXTURE_FLAGS_SPARSE : |
| 50 | 0; |
| 51 | |
| 52 | if (info.GetTileMode() == TileMode_Linear) { |
| 53 | NVNdeviceInfo strideAlignmentInfo; |
| 54 | if (info.GetGpuAccessFlags() & (GpuAccess_DepthStencil | GpuAccess_ColorBuffer)) { |
| 55 | strideAlignmentInfo = NVN_DEVICE_INFO_LINEAR_RENDER_TARGET_STRIDE_ALIGNMENT; |
| 56 | flags |= NVN_TEXTURE_FLAGS_LINEAR_RENDER_TARGET; |
| 57 | |
| 58 | } else { |
| 59 | strideAlignmentInfo = NVN_DEVICE_INFO_LINEAR_TEXTURE_STRIDE_ALIGNMENT; |
| 60 | flags |= NVN_TEXTURE_FLAGS_LINEAR; |
| 61 | } |
| 62 | |
| 63 | int strideAlignment; |
| 64 | nvnDeviceGetInteger(device: pDevice, pname: strideAlignmentInfo, v: &strideAlignment); |
| 65 | ptrdiff_t stride = nn::util::align_up( |
| 66 | x: CalculateRowSize(info.GetWidth(), GetChannelFormat(format: info.GetImageFormat())), |
| 67 | align: strideAlignment); |
| 68 | nvnTextureBuilderSetStride(builder: pBuilder, stride); |
| 69 | } |
| 70 | |
| 71 | nvnTextureBuilderSetFlags(builder: pBuilder, flags); |
| 72 | if (info.ToData()->flags.GetBit(p: info.ToData()->Flag_SpecifyTextureLayout)) { |
| 73 | auto pLayout = |
| 74 | reinterpret_cast<const NVNpackagedTextureLayout*>(info.ToData()->textureLayout); |
| 75 | nvnTextureBuilderSetPackagedTextureLayout(builder: pBuilder, layout: pLayout); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | size_t CalculateMipDataOffsetSize(ptrdiff_t* pMipOffsets, const TextureInfo& info) { |
| 80 | ImageStorageDimension imageStorageDimension = info.GetImageStorageDimension(); |
| 81 | int arrayLength = info.GetArrayLength(); |
| 82 | |
| 83 | int width = info.GetWidth(); |
| 84 | int height = (imageStorageDimension == ImageStorageDimension_1d && arrayLength > 1) ? |
| 85 | arrayLength : |
| 86 | info.GetHeight(); |
| 87 | int depth = (imageStorageDimension == ImageStorageDimension_2d && arrayLength > 1) ? |
| 88 | arrayLength : |
| 89 | info.GetDepth(); |
| 90 | |
| 91 | int minHeight = 1; |
| 92 | int minDepth = 1; |
| 93 | if (arrayLength > 1) { |
| 94 | if (imageStorageDimension == ImageStorageDimension_1d) { |
| 95 | minHeight = height; |
| 96 | } else if (imageStorageDimension == ImageStorageDimension_2d) { |
| 97 | minDepth = depth; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | ChannelFormat channelFormat = GetChannelFormat(format: info.GetImageFormat()); |
| 102 | size_t size = 0; |
| 103 | |
| 104 | for (size_t mipLevel = 0; mipLevel < static_cast<size_t>(info.GetMipCount()); ++mipLevel) { |
| 105 | if (pMipOffsets) { |
| 106 | pMipOffsets[mipLevel] = size; |
| 107 | } |
| 108 | |
| 109 | size += CalculateImageSize(channelFormat, std::max(a: width >> mipLevel, b: 1), |
| 110 | std::max(a: height >> mipLevel, b: minHeight), |
| 111 | std::max(a: depth >> mipLevel, b: minDepth)); |
| 112 | } |
| 113 | return size; |
| 114 | } |
| 115 | |
| 116 | } // namespace |
| 117 | |
| 118 | size_t |
| 119 | TextureImpl<ApiVariationNvn8>::CalculateMipDataAlignment(DeviceImpl<ApiVariationNvn8>* pNnDevice, |
| 120 | const TextureInfo& info) { |
| 121 | NVNtextureBuilder builder; |
| 122 | SetupTextureBuilder(pBuilder: &builder, pDevice: pNnDevice->ToData()->pNvnDevice, info); |
| 123 | return nvnTextureBuilderGetStorageAlignment(builder: &builder); |
| 124 | } |
| 125 | |
| 126 | size_t TextureImpl<ApiVariationNvn8>::CalculateMipDataSize(DeviceImpl<ApiVariationNvn8>* pNnDevice, |
| 127 | const TextureInfo& info) { |
| 128 | NVNtextureBuilder builder; |
| 129 | SetupTextureBuilder(pBuilder: &builder, pDevice: pNnDevice->ToData()->pNvnDevice, info); |
| 130 | return nvnTextureBuilderGetStorageSize(builder: &builder); |
| 131 | } |
| 132 | |
| 133 | void TextureImpl<ApiVariationNvn8>::CalculateMipDataOffsets( |
| 134 | ptrdiff_t* pMipOffsets, [[maybe_unused]] DeviceImpl<ApiVariationNvn8>* pNnDevice, |
| 135 | const TextureInfo& info) { |
| 136 | CalculateMipDataOffsetSize(pMipOffsets, info); |
| 137 | } |
| 138 | |
| 139 | size_t TextureImpl<ApiVariationNvn8>::GetRowPitch(DeviceImpl<ApiVariationNvn8>* pDevice, |
| 140 | const TextureInfo& info) { |
| 141 | int stride; |
| 142 | nvnDeviceGetInteger( |
| 143 | device: pDevice->ToData()->pNvnDevice, |
| 144 | pname: (info.GetGpuAccessFlags() & (GpuAccess_DepthStencil | GpuAccess_ColorBuffer)) ? |
| 145 | NVN_DEVICE_INFO_LINEAR_RENDER_TARGET_STRIDE_ALIGNMENT : |
| 146 | NVN_DEVICE_INFO_LINEAR_TEXTURE_STRIDE_ALIGNMENT, |
| 147 | v: &stride); |
| 148 | |
| 149 | size_t row = CalculateRowSize(info.GetWidth(), GetChannelFormat(format: info.GetImageFormat())); |
| 150 | return nn::util::align_up(x: row, align: stride); |
| 151 | } |
| 152 | |
| 153 | TextureImpl<ApiVariationNvn8>::TextureImpl() { |
| 154 | state = State_NotInitialized; |
| 155 | } |
| 156 | |
| 157 | TextureImpl<ApiVariationNvn8>::~TextureImpl() {} |
| 158 | |
| 159 | void TextureImpl<ApiVariationNvn8>::Initialize(DeviceImpl<ApiVariationNvn8>* pDevice, |
| 160 | const TextureInfo& info, |
| 161 | MemoryPoolImpl<ApiVariationNvn8>* pMemoryPool, |
| 162 | ptrdiff_t memoryPoolOffset, size_t memoryPoolSize) { |
| 163 | NVNtextureBuilder textureBuilder; |
| 164 | SetupTextureBuilder(pBuilder: &textureBuilder, pDevice: pDevice->ToData()->pNvnDevice, info); |
| 165 | nvnTextureBuilderSetStorage(builder: &textureBuilder, pool: pMemoryPool->ToData()->pNvnMemoryPool, |
| 166 | offset: memoryPoolOffset); |
| 167 | |
| 168 | Nvn::SetPackagedTextureDataImpl(&textureBuilder, info, pMemoryPool, memoryPoolOffset, |
| 169 | memoryPoolSize); |
| 170 | |
| 171 | pNvnTexture = &nvnTexture; |
| 172 | |
| 173 | nvnTextureInitialize(texture: pNvnTexture, builder: &textureBuilder); |
| 174 | |
| 175 | flags.SetBit(p: Flag_Shared, on: false); |
| 176 | state = State_Initialized; |
| 177 | } |
| 178 | |
| 179 | void TextureImpl<ApiVariationNvn8>::Finalize(DeviceImpl<ApiVariationNvn8>*) { |
| 180 | nvnTextureFinalize(texture: pNvnTexture); |
| 181 | pNvnTexture = nullptr; |
| 182 | state = State_NotInitialized; |
| 183 | } |
| 184 | |
| 185 | TextureViewImpl<ApiVariationNvn8>::TextureViewImpl() { |
| 186 | state = State_NotInitialized; |
| 187 | } |
| 188 | |
| 189 | TextureViewImpl<ApiVariationNvn8>::~TextureViewImpl() {} |
| 190 | |
| 191 | void TextureViewImpl<ApiVariationNvn8>::Initialize(DeviceImpl<ApiVariationNvn8>*, |
| 192 | const TextureViewInfo& info) { |
| 193 | const TextureImpl<ApiVariationNvn8>* pSourceTexture = info.GetTexturePtr(); |
| 194 | |
| 195 | pNvnTexture = pSourceTexture->ToData()->pNvnTexture; |
| 196 | pNvnTextureView = &nvnTextureView; |
| 197 | |
| 198 | const NVNtextureSwizzle s_ChannelMappingTable[6] = { |
| 199 | NVN_TEXTURE_SWIZZLE_ZERO, NVN_TEXTURE_SWIZZLE_ONE, NVN_TEXTURE_SWIZZLE_R, |
| 200 | NVN_TEXTURE_SWIZZLE_G, NVN_TEXTURE_SWIZZLE_B, NVN_TEXTURE_SWIZZLE_A, |
| 201 | }; |
| 202 | |
| 203 | nvnTextureViewSetDefaults(view: pNvnTextureView); |
| 204 | nvnTextureViewSetLevels(view: pNvnTextureView, |
| 205 | baseLevel: info.GetSubresourceRange().GetMipRange().GetMinMipLevel(), |
| 206 | numLevels: info.GetSubresourceRange().GetMipRange().GetMipCount()); |
| 207 | |
| 208 | if (info.GetImageDimension() != ImageDimension_3d) { |
| 209 | nvnTextureViewSetLayers(view: pNvnTextureView, |
| 210 | minLayer: info.GetSubresourceRange().GetArrayRange().GetBaseArrayIndex(), |
| 211 | numLayers: info.GetSubresourceRange().GetArrayRange().GetArrayLength()); |
| 212 | } |
| 213 | |
| 214 | #if NN_SDK_VER > NN_MAKE_VER(3, 5, 1) |
| 215 | Nvn::SetTextureViewFormat(pNvnTextureView, Nvn::GetImageFormat(info.GetImageFormat()), |
| 216 | pNvnTexture); |
| 217 | #else |
| 218 | nvnTextureViewSetFormat(view: pNvnTextureView, format: Nvn::GetImageFormat(info.GetImageFormat())); |
| 219 | #endif |
| 220 | |
| 221 | nvnTextureViewSetSwizzle(view: pNvnTextureView, |
| 222 | r: s_ChannelMappingTable[info.GetChannelMapping(channel: ColorChannel_Red)], |
| 223 | g: s_ChannelMappingTable[info.GetChannelMapping(channel: ColorChannel_Green)], |
| 224 | b: s_ChannelMappingTable[info.GetChannelMapping(channel: ColorChannel_Blue)], |
| 225 | a: s_ChannelMappingTable[info.GetChannelMapping(channel: ColorChannel_Alpha)]); |
| 226 | nvnTextureViewSetDepthStencilMode(view: pNvnTextureView, mode: (info.GetDepthStencilTextureMode() != |
| 227 | DepthStencilFetchMode_DepthComponent) ? |
| 228 | NVN_TEXTURE_DEPTH_STENCIL_MODE_STENCIL : |
| 229 | NVN_TEXTURE_DEPTH_STENCIL_MODE_DEPTH); |
| 230 | |
| 231 | nvnTextureViewSetTarget(view: pNvnTextureView, target: Nvn::GetImageTarget(info.GetImageDimension())); |
| 232 | |
| 233 | flags.SetBit(p: Flag_Shared, on: false); |
| 234 | state = State_Initialized; |
| 235 | } |
| 236 | |
| 237 | void TextureViewImpl<ApiVariationNvn8>::Finalize(DeviceImpl<ApiVariationNvn8>*) { |
| 238 | state = State_NotInitialized; |
| 239 | pNvnTexture = nullptr; |
| 240 | pNvnTextureView = nullptr; |
| 241 | } |
| 242 | |
| 243 | ColorTargetViewImpl<ApiVariationNvn8>::ColorTargetViewImpl() { |
| 244 | state = State_NotInitialized; |
| 245 | } |
| 246 | |
| 247 | ColorTargetViewImpl<ApiVariationNvn8>::~ColorTargetViewImpl() {} |
| 248 | |
| 249 | void ColorTargetViewImpl<ApiVariationNvn8>::Initialize(DeviceImpl<ApiVariationNvn8>*, |
| 250 | const ColorTargetViewInfo& info) { |
| 251 | const TextureImpl<ApiVariationNvn8>* pSourceTexture = info.GetTexturePtr(); |
| 252 | |
| 253 | pNvnTexture = pSourceTexture->ToData()->pNvnTexture; |
| 254 | pNvnTextureView = &nvnTextureView; |
| 255 | |
| 256 | nvnTextureViewSetDefaults(view: pNvnTextureView); |
| 257 | nvnTextureViewSetLevels(view: pNvnTextureView, baseLevel: info.GetMipLevel(), numLevels: 1); |
| 258 | nvnTextureViewSetLayers(view: pNvnTextureView, minLayer: info.GetArrayRange().GetBaseArrayIndex(), |
| 259 | numLayers: info.GetArrayRange().GetArrayLength()); |
| 260 | |
| 261 | nvnTextureViewSetFormat(view: pNvnTextureView, format: Nvn::GetImageFormat(info.GetImageFormat())); |
| 262 | nvnTextureViewSetTarget(view: pNvnTextureView, target: Nvn::GetImageTarget(info.GetImageDimension())); |
| 263 | |
| 264 | flags.SetBit(p: Flag_Shared, on: false); |
| 265 | state = State_Initialized; |
| 266 | } |
| 267 | |
| 268 | void ColorTargetViewImpl<ApiVariationNvn8>::Finalize(DeviceImpl<ApiVariationNvn8>*) { |
| 269 | state = State_NotInitialized; |
| 270 | pNvnTexture = nullptr; |
| 271 | pNvnTextureView = nullptr; |
| 272 | } |
| 273 | |
| 274 | DepthStencilViewImpl<ApiVariationNvn8>::DepthStencilViewImpl() { |
| 275 | state = State_NotInitialized; |
| 276 | } |
| 277 | |
| 278 | DepthStencilViewImpl<ApiVariationNvn8>::~DepthStencilViewImpl() {} |
| 279 | |
| 280 | void DepthStencilViewImpl<ApiVariationNvn8>::Initialize(DeviceImpl<ApiVariationNvn8>*, |
| 281 | const DepthStencilViewInfo& info) { |
| 282 | const TextureImpl<ApiVariationNvn8>* pSourceTexture = info.GetTexturePtr(); |
| 283 | |
| 284 | pNvnTexture = pSourceTexture->ToData()->pNvnTexture; |
| 285 | pNvnTextureView = &nvnTextureView; |
| 286 | |
| 287 | nvnTextureViewSetDefaults(view: pNvnTextureView); |
| 288 | nvnTextureViewSetLevels(view: pNvnTextureView, baseLevel: info.GetMipLevel(), numLevels: 1); |
| 289 | nvnTextureViewSetLayers(view: pNvnTextureView, minLayer: info.GetArrayRange().GetBaseArrayIndex(), |
| 290 | numLayers: info.GetArrayRange().GetArrayLength()); |
| 291 | |
| 292 | nvnTextureViewSetTarget(view: pNvnTextureView, target: Nvn::GetImageTarget(info.GetImageDimension())); |
| 293 | |
| 294 | flags.SetBit(p: Flag_Shared, on: false); |
| 295 | state = State_Initialized; |
| 296 | } |
| 297 | |
| 298 | void DepthStencilViewImpl<ApiVariationNvn8>::Finalize(DeviceImpl<ApiVariationNvn8>*) { |
| 299 | state = State_NotInitialized; |
| 300 | pNvnTexture = nullptr; |
| 301 | pNvnTextureView = nullptr; |
| 302 | } |
| 303 | |
| 304 | template <> |
| 305 | void GetImageFormatProperty<ApiVariationNvn8>( |
| 306 | ImageFormatProperty* pOutImageFormatProperty, |
| 307 | [[maybe_unused]] DeviceImpl<ApiVariationNvn8>* pDevice, ImageFormat imageFormat) { |
| 308 | NVNformat nvnFormat = Nvn::GetImageFormat(imageFormat); |
| 309 | Nvn::GetImageFormatProperty(pOutImageFormatProperty, nvnFormat); |
| 310 | } |
| 311 | |
| 312 | } // namespace nn::gfx::detail |