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
180 lines (152 loc) · 6.03 KB
/
Copy pathTexture.h
File metadata and controls
180 lines (152 loc) · 6.03 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//===- 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 {
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
class Device;
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;
// Calculate the size in bytes of the texture data given a linear layout
// Useful for calculating the size for an upload or readback buffer.
size_t calculateLinearSizeInBytes(Device &Dev) const;
// Maps the texture's memory for host access. Only valid for CpuToGpu and
// GpuToCpu textures; returns an error for GpuOnly. Each successful map() must
// be paired with a call to unmap() before the texture is used on the GPU.
virtual llvm::Expected<void *> map() = 0;
virtual void unmap() = 0;
GPUAPI getAPI() const { return API; }
virtual const TextureCreateDesc &getDesc() const = 0;
// The byte stride between consecutive rows when the texture is mapped for
// direct host access. Errors if the texture is not host-visible, or if its
// memory layout is not linear (the mapped bytes have no well-defined row
// stride otherwise).
virtual llvm::Expected<uint32_t> getMappedRowPitchInBytes() const = 0;
protected:
explicit Texture(GPUAPI API) : API(API) {}
};
} // namespace offloadtest
#endif // OFFLOADTEST_API_TEXTURE_H