forked from llvm/offload-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.h
More file actions
160 lines (136 loc) · 5.1 KB
/
Texture.h
File metadata and controls
160 lines (136 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//===- Texture.h - Offload API Texture ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//
#ifndef OFFLOADTEST_API_TEXTURE_H
#define OFFLOADTEST_API_TEXTURE_H
#include "API/API.h"
#include "API/Resources.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Error.h"
#include <cstdint>
#include <optional>
#include <string>
#include <variant>
namespace offloadtest {
enum TextureUsage : uint32_t {
Sampled = 1 << 0,
Storage = 1 << 1,
RenderTarget = 1 << 2,
DepthStencil = 1 << 3,
LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ DepthStencil)
};
inline std::string getTextureUsageName(TextureUsage Usage) {
std::string Result;
if ((Usage & Sampled) != 0)
Result += "Sampled|";
if ((Usage & Storage) != 0)
Result += "Storage|";
if ((Usage & RenderTarget) != 0)
Result += "RenderTarget|";
if ((Usage & DepthStencil) != 0)
Result += "DepthStencil|";
if (!Result.empty())
Result.pop_back(); // Remove trailing '|'
return Result;
}
struct ClearColor {
float R = 0.0f, G = 0.0f, B = 0.0f, A = 0.0f;
};
struct ClearDepthStencil {
float Depth = 1.0f;
uint8_t Stencil = 0;
};
using ClearValue = std::variant<ClearColor, ClearDepthStencil>;
// TODO: Currently only 2D textures are supported. When expanding to 1D, 3D,
// cube, or array textures, add a TextureType enum and validation between usage
// and type (e.g. 3D textures cannot be used as DepthStencil).
struct TextureCreateDesc {
MemoryLocation Location;
TextureUsage Usage;
Format Fmt;
uint32_t Width;
uint32_t Height;
uint32_t MipLevels;
// Clear value for render target or depth/stencil textures.
// How and when this is applied depends on the backend:
// - DX uses it as an optimized clear hint at resource creation time
// - VK and MTL apply it at render pass begin
std::optional<ClearValue> OptimizedClearValue;
};
inline llvm::Error validateTextureCreateDesc(const TextureCreateDesc &Desc) {
if (!isTextureCompatible(Desc.Fmt))
return llvm::createStringError(
std::errc::invalid_argument,
"Format '%s' is not compatible with texture creation.",
getFormatName(Desc.Fmt).data());
const bool IsDepth = isDepthFormat(Desc.Fmt);
const bool IsRT = (Desc.Usage & TextureUsage::RenderTarget) != 0;
const bool IsDS = (Desc.Usage & TextureUsage::DepthStencil) != 0;
// DepthStencil + RenderTarget is not supported.
if (IsDS && IsRT)
return llvm::createStringError(
std::errc::invalid_argument,
"DepthStencil and RenderTarget are mutually exclusive.");
// DepthStencil + Storage is a valid but discouraged configuration (poor
// performance on most hardware). Not supported for now.
if (IsDS && (Desc.Usage & TextureUsage::Storage) != 0)
return llvm::createStringError(
std::errc::not_supported,
"DepthStencil combined with Storage is not yet supported.");
// Depth formats require DepthStencil usage; non-depth formats forbid it.
if (IsDepth && !IsDS)
return llvm::createStringError(
std::errc::invalid_argument,
"Depth format '%s' requires DepthStencil usage.",
getFormatName(Desc.Fmt).data());
if (!IsDepth && IsDS)
return llvm::createStringError(
std::errc::invalid_argument,
"DepthStencil usage requires a depth format, got '%s'.",
getFormatName(Desc.Fmt).data());
// Render targets and depth/stencil textures only support a single mip level.
if ((IsRT || IsDS) && Desc.MipLevels != 1)
return llvm::createStringError(
std::errc::not_supported,
"Multiple mip levels are not supported for render target or "
"depth/stencil textures.");
// A clear value requires RenderTarget or DepthStencil usage, and the
// variant must match.
if (Desc.OptimizedClearValue) {
if (!IsRT && !IsDS)
return llvm::createStringError(
std::errc::invalid_argument,
"OptimizedClearValue requires RenderTarget or DepthStencil usage.");
if (IsRT && !std::holds_alternative<ClearColor>(*Desc.OptimizedClearValue))
return llvm::createStringError(
std::errc::invalid_argument,
"RenderTarget usage requires a ClearColor clear value.");
if (IsDS &&
!std::holds_alternative<ClearDepthStencil>(*Desc.OptimizedClearValue))
return llvm::createStringError(
std::errc::invalid_argument,
"DepthStencil usage requires a ClearDepthStencil clear value.");
}
return llvm::Error::success();
}
class Texture {
GPUAPI API;
public:
virtual ~Texture();
Texture(const Texture &) = delete;
Texture &operator=(const Texture &) = delete;
GPUAPI getAPI() const { return API; }
protected:
explicit Texture(GPUAPI API) : API(API) {}
};
} // namespace offloadtest
#endif // OFFLOADTEST_API_TEXTURE_H