Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/microsoft/clc/compute_test.h
4560 views
1
/*
2
* Copyright © Microsoft Corporation
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
*/
23
24
#include <stdio.h>
25
#include <stdint.h>
26
#include <stdexcept>
27
28
#include <directx/d3d12.h>
29
#include <dxgi1_4.h>
30
#include <gtest/gtest.h>
31
#include <wrl.h>
32
33
#include "clc_compiler.h"
34
35
using std::runtime_error;
36
using Microsoft::WRL::ComPtr;
37
38
inline D3D12_CPU_DESCRIPTOR_HANDLE
39
offset_cpu_handle(D3D12_CPU_DESCRIPTOR_HANDLE handle, UINT offset)
40
{
41
handle.ptr += offset;
42
return handle;
43
}
44
45
inline size_t
46
align(size_t value, unsigned alignment)
47
{
48
assert(alignment > 0);
49
return ((value + (alignment - 1)) / alignment) * alignment;
50
}
51
52
class ComputeTest : public ::testing::Test {
53
protected:
54
struct Shader {
55
std::shared_ptr<struct clc_object> obj;
56
std::shared_ptr<struct clc_dxil_object> dxil;
57
};
58
59
static void
60
enable_d3d12_debug_layer();
61
62
static IDXGIFactory4 *
63
get_dxgi_factory();
64
65
static IDXGIAdapter1 *
66
choose_adapter(IDXGIFactory4 *factory);
67
68
static ID3D12Device *
69
create_device(IDXGIAdapter1 *adapter);
70
71
struct Resources {
72
void add(ComPtr<ID3D12Resource> res,
73
D3D12_DESCRIPTOR_RANGE_TYPE type,
74
unsigned spaceid,
75
unsigned resid)
76
{
77
descs.push_back(res);
78
79
if(!ranges.empty() &&
80
ranges.back().RangeType == type &&
81
ranges.back().RegisterSpace == spaceid &&
82
ranges.back().BaseShaderRegister + ranges.back().NumDescriptors == resid) {
83
ranges.back().NumDescriptors++;
84
return;
85
}
86
87
D3D12_DESCRIPTOR_RANGE1 range;
88
89
range.RangeType = type;
90
range.NumDescriptors = 1;
91
range.BaseShaderRegister = resid;
92
range.RegisterSpace = spaceid;
93
range.OffsetInDescriptorsFromTableStart = descs.size() - 1;
94
range.Flags = D3D12_DESCRIPTOR_RANGE_FLAG_DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS;
95
ranges.push_back(range);
96
}
97
98
std::vector<D3D12_DESCRIPTOR_RANGE1> ranges;
99
std::vector<ComPtr<ID3D12Resource>> descs;
100
};
101
102
ComPtr<ID3D12RootSignature>
103
create_root_signature(const Resources &resources);
104
105
ComPtr<ID3D12PipelineState>
106
create_pipeline_state(ComPtr<ID3D12RootSignature> &root_sig,
107
const struct clc_dxil_object &dxil);
108
109
ComPtr<ID3D12Resource>
110
create_buffer(int size, D3D12_HEAP_TYPE heap_type);
111
112
ComPtr<ID3D12Resource>
113
create_upload_buffer_with_data(const void *data, size_t size);
114
115
ComPtr<ID3D12Resource>
116
create_sized_buffer_with_data(size_t buffer_size, const void *data,
117
size_t data_size);
118
119
ComPtr<ID3D12Resource>
120
create_buffer_with_data(const void *data, size_t size)
121
{
122
return create_sized_buffer_with_data(size, data, size);
123
}
124
125
void
126
get_buffer_data(ComPtr<ID3D12Resource> res,
127
void *buf, size_t size);
128
129
void
130
resource_barrier(ComPtr<ID3D12Resource> &res,
131
D3D12_RESOURCE_STATES state_before,
132
D3D12_RESOURCE_STATES state_after);
133
134
void
135
execute_cmdlist();
136
137
void
138
create_uav_buffer(ComPtr<ID3D12Resource> res,
139
size_t width, size_t byte_stride,
140
D3D12_CPU_DESCRIPTOR_HANDLE cpu_handle);
141
142
void create_cbv(ComPtr<ID3D12Resource> res, size_t size,
143
D3D12_CPU_DESCRIPTOR_HANDLE cpu_handle);
144
145
ComPtr<ID3D12Resource>
146
add_uav_resource(Resources &resources, unsigned spaceid, unsigned resid,
147
const void *data = NULL, size_t num_elems = 0,
148
size_t elem_size = 0);
149
150
ComPtr<ID3D12Resource>
151
add_cbv_resource(Resources &resources, unsigned spaceid, unsigned resid,
152
const void *data, size_t size);
153
154
void
155
SetUp() override;
156
157
void
158
TearDown() override;
159
160
Shader
161
compile(const std::vector<const char *> &sources,
162
const std::vector<const char *> &compile_args = {},
163
bool create_library = false);
164
165
Shader
166
link(const std::vector<Shader> &sources,
167
bool create_library = false);
168
169
void
170
configure(Shader &shader,
171
const struct clc_runtime_kernel_conf *conf);
172
173
void
174
validate(Shader &shader);
175
176
enum ShaderArgDirection {
177
SHADER_ARG_INPUT = 1,
178
SHADER_ARG_OUTPUT = 2,
179
SHADER_ARG_INOUT = SHADER_ARG_INPUT | SHADER_ARG_OUTPUT,
180
};
181
182
class RawShaderArg {
183
public:
184
RawShaderArg(enum ShaderArgDirection dir) : dir(dir) { }
185
virtual size_t get_elem_size() const = 0;
186
virtual size_t get_num_elems() const = 0;
187
virtual const void *get_data() const = 0;
188
virtual void *get_data() = 0;
189
enum ShaderArgDirection get_direction() { return dir; }
190
private:
191
enum ShaderArgDirection dir;
192
};
193
194
class NullShaderArg : public RawShaderArg {
195
public:
196
NullShaderArg() : RawShaderArg(SHADER_ARG_INPUT) { }
197
size_t get_elem_size() const override { return 0; }
198
size_t get_num_elems() const override { return 0; }
199
const void *get_data() const override { return NULL; }
200
void *get_data() override { return NULL; }
201
};
202
203
template <typename T>
204
class ShaderArg : public std::vector<T>, public RawShaderArg
205
{
206
public:
207
ShaderArg(const T &v, enum ShaderArgDirection dir = SHADER_ARG_INOUT) :
208
std::vector<T>({ v }), RawShaderArg(dir) { }
209
ShaderArg(const std::vector<T> &v, enum ShaderArgDirection dir = SHADER_ARG_INOUT) :
210
std::vector<T>(v), RawShaderArg(dir) { }
211
ShaderArg(const std::initializer_list<T> v, enum ShaderArgDirection dir = SHADER_ARG_INOUT) :
212
std::vector<T>(v), RawShaderArg(dir) { }
213
214
ShaderArg<T>& operator =(const T &v)
215
{
216
this->clear();
217
this->push_back(v);
218
return *this;
219
}
220
221
operator T&() { return this->at(0); }
222
operator const T&() const { return this->at(0); }
223
224
ShaderArg<T>& operator =(const std::vector<T> &v)
225
{
226
*this = v;
227
return *this;
228
}
229
230
ShaderArg<T>& operator =(std::initializer_list<T> v)
231
{
232
*this = v;
233
return *this;
234
}
235
236
size_t get_elem_size() const override { return sizeof(T); }
237
size_t get_num_elems() const override { return this->size(); }
238
const void *get_data() const override { return this->data(); }
239
void *get_data() override { return this->data(); }
240
};
241
242
struct CompileArgs
243
{
244
unsigned x, y, z;
245
std::vector<const char *> compiler_command_line;
246
clc_work_properties_data work_props;
247
};
248
249
private:
250
void gather_args(std::vector<RawShaderArg *> &args) { }
251
252
template <typename T, typename... Rest>
253
void gather_args(std::vector<RawShaderArg *> &args, T &arg, Rest&... rest)
254
{
255
args.push_back(&arg);
256
gather_args(args, rest...);
257
}
258
259
void run_shader_with_raw_args(Shader shader,
260
const CompileArgs &compile_args,
261
const std::vector<RawShaderArg *> &args);
262
263
protected:
264
template <typename... Args>
265
void run_shader(Shader shader,
266
const CompileArgs &compile_args,
267
Args&... args)
268
{
269
std::vector<RawShaderArg *> raw_args;
270
gather_args(raw_args, args...);
271
run_shader_with_raw_args(shader, compile_args, raw_args);
272
}
273
274
template <typename... Args>
275
void run_shader(const std::vector<const char *> &sources,
276
unsigned x, unsigned y, unsigned z,
277
Args&... args)
278
{
279
std::vector<RawShaderArg *> raw_args;
280
gather_args(raw_args, args...);
281
CompileArgs compile_args = { x, y, z };
282
run_shader_with_raw_args(compile(sources), compile_args, raw_args);
283
}
284
285
template <typename... Args>
286
void run_shader(const std::vector<const char *> &sources,
287
const CompileArgs &compile_args,
288
Args&... args)
289
{
290
std::vector<RawShaderArg *> raw_args;
291
gather_args(raw_args, args...);
292
run_shader_with_raw_args(
293
compile(sources, compile_args.compiler_command_line),
294
compile_args, raw_args);
295
}
296
297
template <typename... Args>
298
void run_shader(const char *source,
299
unsigned x, unsigned y, unsigned z,
300
Args&... args)
301
{
302
std::vector<RawShaderArg *> raw_args;
303
gather_args(raw_args, args...);
304
CompileArgs compile_args = { x, y, z };
305
run_shader_with_raw_args(compile({ source }), compile_args, raw_args);
306
}
307
308
IDXGIFactory4 *factory;
309
IDXGIAdapter1 *adapter;
310
ID3D12Device *dev;
311
ID3D12Fence *cmdqueue_fence;
312
ID3D12CommandQueue *cmdqueue;
313
ID3D12CommandAllocator *cmdalloc;
314
ID3D12GraphicsCommandList *cmdlist;
315
ID3D12DescriptorHeap *uav_heap;
316
317
struct clc_context *compiler_ctx;
318
319
UINT uav_heap_incr;
320
int fence_value;
321
322
HANDLE event;
323
static PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE D3D12SerializeVersionedRootSignature;
324
};
325
326