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 "d3d12_bufmgr.h"
25 #include "d3d12_context.h"
26 #include "d3d12_format.h"
27 #include "d3d12_screen.h"
28
29 #include "pipebuffer/pb_buffer.h"
30 #include "pipebuffer/pb_bufmgr.h"
31
32 #include "util/format/u_format.h"
33 #include "util/u_memory.h"
34
35 #include <dxguids/dxguids.h>
36
37 struct d3d12_bufmgr {
38 struct pb_manager base;
39
40 struct d3d12_screen *screen;
41 };
42
43 extern const struct pb_vtbl d3d12_buffer_vtbl;
44
45 static inline struct d3d12_bufmgr *
d3d12_bufmgr(struct pb_manager * mgr)46 d3d12_bufmgr(struct pb_manager *mgr)
47 {
48 assert(mgr);
49
50 return (struct d3d12_bufmgr *)mgr;
51 }
52
53 static void
describe_direct_bo(char * buf,struct d3d12_bo * ptr)54 describe_direct_bo(char *buf, struct d3d12_bo *ptr)
55 {
56 sprintf(buf, "d3d12_bo<direct,%p,0x%x>", ptr->res, (unsigned)ptr->estimated_size);
57 }
58
59 static void
describe_suballoc_bo(char * buf,struct d3d12_bo * ptr)60 describe_suballoc_bo(char *buf, struct d3d12_bo *ptr)
61 {
62 char res[128];
63 uint64_t offset;
64 d3d12_bo *base = d3d12_bo_get_base(ptr, &offset);
65 describe_direct_bo(res, base);
66 sprintf(buf, "d3d12_bo<suballoc<%s>,0x%x,0x%x>", res,
67 (unsigned)ptr->buffer->size, (unsigned)offset);
68 }
69
70 void
d3d12_debug_describe_bo(char * buf,struct d3d12_bo * ptr)71 d3d12_debug_describe_bo(char *buf, struct d3d12_bo *ptr)
72 {
73 if (ptr->buffer)
74 describe_suballoc_bo(buf, ptr);
75 else
76 describe_direct_bo(buf, ptr);
77 }
78
79 struct d3d12_bo *
d3d12_bo_wrap_res(struct d3d12_screen * screen,ID3D12Resource * res,enum d3d12_residency_status residency)80 d3d12_bo_wrap_res(struct d3d12_screen *screen, ID3D12Resource *res, enum d3d12_residency_status residency)
81 {
82 struct d3d12_bo *bo;
83
84 bo = CALLOC_STRUCT(d3d12_bo);
85 if (!bo)
86 return NULL;
87
88 D3D12_RESOURCE_DESC desc = GetDesc(res);
89 unsigned array_size = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? 1 : desc.DepthOrArraySize;
90 unsigned total_subresources = desc.MipLevels * array_size * d3d12_non_opaque_plane_count(desc.Format);
91 bool supports_simultaneous_access = d3d12_resource_supports_simultaneous_access(&desc);
92
93 pipe_reference_init(&bo->reference, 1);
94 bo->screen = screen;
95 bo->res = res;
96 bo->unique_id = p_atomic_inc_return(&screen->resource_id_generator);
97 if (!supports_simultaneous_access)
98 d3d12_resource_state_init(&bo->global_state, total_subresources, false);
99
100 bo->residency_status = residency;
101 bo->last_used_timestamp = 0;
102 screen->dev->GetCopyableFootprints(&desc, 0, total_subresources, 0, nullptr, nullptr, nullptr, &bo->estimated_size);
103 if (residency != d3d12_evicted) {
104 mtx_lock(&screen->submit_mutex);
105 list_add(&bo->residency_list_entry, &screen->residency_list);
106 mtx_unlock(&screen->submit_mutex);
107 }
108
109 return bo;
110 }
111
112 struct d3d12_bo *
d3d12_bo_new(struct d3d12_screen * screen,uint64_t size,const pb_desc * pb_desc)113 d3d12_bo_new(struct d3d12_screen *screen, uint64_t size, const pb_desc *pb_desc)
114 {
115 ID3D12Device *dev = screen->dev;
116 ID3D12Resource *res;
117
118 D3D12_RESOURCE_DESC res_desc;
119 res_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
120 res_desc.Format = DXGI_FORMAT_UNKNOWN;
121 res_desc.Alignment = 0;
122 res_desc.Width = size;
123 res_desc.Height = 1;
124 res_desc.DepthOrArraySize = 1;
125 res_desc.MipLevels = 1;
126 res_desc.SampleDesc.Count = 1;
127 res_desc.SampleDesc.Quality = 0;
128 res_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
129 res_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
130
131 D3D12_HEAP_TYPE heap_type = D3D12_HEAP_TYPE_DEFAULT;
132 if (pb_desc->usage & PB_USAGE_CPU_READ)
133 heap_type = D3D12_HEAP_TYPE_READBACK;
134 else if (pb_desc->usage & PB_USAGE_CPU_WRITE)
135 heap_type = D3D12_HEAP_TYPE_UPLOAD;
136
137 D3D12_HEAP_FLAGS heap_flags = screen->support_create_not_resident ?
138 D3D12_HEAP_FLAG_CREATE_NOT_RESIDENT : D3D12_HEAP_FLAG_NONE;
139 enum d3d12_residency_status init_residency = screen->support_create_not_resident ?
140 d3d12_evicted : d3d12_resident;
141
142 D3D12_HEAP_PROPERTIES heap_pris = GetCustomHeapProperties(dev, heap_type);
143 HRESULT hres = dev->CreateCommittedResource(&heap_pris,
144 heap_flags,
145 &res_desc,
146 D3D12_RESOURCE_STATE_COMMON,
147 NULL,
148 IID_PPV_ARGS(&res));
149
150 if (FAILED(hres))
151 return NULL;
152
153 return d3d12_bo_wrap_res(screen, res, init_residency);
154 }
155
156 struct d3d12_bo *
d3d12_bo_wrap_buffer(struct d3d12_screen * screen,struct pb_buffer * buf)157 d3d12_bo_wrap_buffer(struct d3d12_screen *screen, struct pb_buffer *buf)
158 {
159 struct d3d12_bo *bo;
160
161 bo = CALLOC_STRUCT(d3d12_bo);
162 if (!bo)
163 return NULL;
164
165 pipe_reference_init(&bo->reference, 1);
166 bo->screen = screen;
167 bo->buffer = buf;
168 bo->unique_id = p_atomic_inc_return(&screen->resource_id_generator);
169 bo->residency_status = d3d12_evicted;
170
171 return bo;
172 }
173
174 void
d3d12_bo_unreference(struct d3d12_bo * bo)175 d3d12_bo_unreference(struct d3d12_bo *bo)
176 {
177 if (bo == NULL)
178 return;
179
180 assert(pipe_is_referenced(&bo->reference));
181
182 if (pipe_reference_described(&bo->reference, NULL,
183 (debug_reference_descriptor)
184 d3d12_debug_describe_bo)) {
185 if (bo->buffer)
186 pb_reference(&bo->buffer, NULL);
187
188 mtx_lock(&bo->screen->submit_mutex);
189
190 if (bo->residency_status != d3d12_evicted)
191 list_del(&bo->residency_list_entry);
192
193 /* MSVC's offsetof fails when the name is ambiguous between struct and function */
194 typedef struct d3d12_context d3d12_context_type;
195 list_for_each_entry(d3d12_context_type, ctx, &bo->screen->context_list, context_list_entry)
196 util_dynarray_append(&ctx->recently_destroyed_bos, uint64_t, bo->unique_id);
197
198 mtx_unlock(&bo->screen->submit_mutex);
199
200 d3d12_resource_state_cleanup(&bo->global_state);
201 if (bo->res)
202 bo->res->Release();
203 FREE(bo);
204 }
205 }
206
207 void *
d3d12_bo_map(struct d3d12_bo * bo,D3D12_RANGE * range)208 d3d12_bo_map(struct d3d12_bo *bo, D3D12_RANGE *range)
209 {
210 struct d3d12_bo *base_bo;
211 D3D12_RANGE offset_range = {0, 0};
212 uint64_t offset;
213 void *ptr;
214
215 base_bo = d3d12_bo_get_base(bo, &offset);
216
217 if (!range || range->Begin >= range->End) {
218 offset_range.Begin = offset;
219 offset_range.End = offset + d3d12_bo_get_size(bo);
220 range = &offset_range;
221 } else {
222 offset_range.Begin = range->Begin + offset;
223 offset_range.End = range->End + offset;
224 range = &offset_range;
225 }
226
227 if (FAILED(base_bo->res->Map(0, range, &ptr)))
228 return NULL;
229
230 return (uint8_t *)ptr + (range ? range->Begin : 0);
231 }
232
233 void
d3d12_bo_unmap(struct d3d12_bo * bo,D3D12_RANGE * range)234 d3d12_bo_unmap(struct d3d12_bo *bo, D3D12_RANGE *range)
235 {
236 struct d3d12_bo *base_bo;
237 D3D12_RANGE offset_range = {0, 0};
238 uint64_t offset;
239
240 base_bo = d3d12_bo_get_base(bo, &offset);
241
242 if (!range || range->Begin >= range->End) {
243 offset_range.Begin = offset;
244 offset_range.End = offset + d3d12_bo_get_size(bo);
245 range = &offset_range;
246 } else {
247 offset_range.Begin = range->Begin + offset;
248 offset_range.End = range->End + offset;
249 range = &offset_range;
250 }
251
252 base_bo->res->Unmap(0, range);
253 }
254
255 static void
d3d12_buffer_destroy(void * winsys,struct pb_buffer * pbuf)256 d3d12_buffer_destroy(void *winsys, struct pb_buffer *pbuf)
257 {
258 struct d3d12_buffer *buf = d3d12_buffer(pbuf);
259
260 if (buf->map)
261 d3d12_bo_unmap(buf->bo, &buf->range);
262 d3d12_bo_unreference(buf->bo);
263 FREE(buf);
264 }
265
266 static void *
d3d12_buffer_map(struct pb_buffer * pbuf,enum pb_usage_flags flags,void * flush_ctx)267 d3d12_buffer_map(struct pb_buffer *pbuf,
268 enum pb_usage_flags flags,
269 void *flush_ctx)
270 {
271 return d3d12_buffer(pbuf)->map;
272 }
273
274 static void
d3d12_buffer_unmap(struct pb_buffer * pbuf)275 d3d12_buffer_unmap(struct pb_buffer *pbuf)
276 {
277 }
278
279 static void
d3d12_buffer_get_base_buffer(struct pb_buffer * buf,struct pb_buffer ** base_buf,pb_size * offset)280 d3d12_buffer_get_base_buffer(struct pb_buffer *buf,
281 struct pb_buffer **base_buf,
282 pb_size *offset)
283 {
284 *base_buf = buf;
285 *offset = 0;
286 }
287
288 static enum pipe_error
d3d12_buffer_validate(struct pb_buffer * pbuf,struct pb_validate * vl,enum pb_usage_flags flags)289 d3d12_buffer_validate(struct pb_buffer *pbuf,
290 struct pb_validate *vl,
291 enum pb_usage_flags flags )
292 {
293 /* Always pinned */
294 return PIPE_OK;
295 }
296
297 static void
d3d12_buffer_fence(struct pb_buffer * pbuf,struct pipe_fence_handle * fence)298 d3d12_buffer_fence(struct pb_buffer *pbuf,
299 struct pipe_fence_handle *fence )
300 {
301 }
302
303 const struct pb_vtbl d3d12_buffer_vtbl = {
304 d3d12_buffer_destroy,
305 d3d12_buffer_map,
306 d3d12_buffer_unmap,
307 d3d12_buffer_validate,
308 d3d12_buffer_fence,
309 d3d12_buffer_get_base_buffer
310 };
311
312 static struct pb_buffer *
d3d12_bufmgr_create_buffer(struct pb_manager * pmgr,pb_size size,const struct pb_desc * pb_desc)313 d3d12_bufmgr_create_buffer(struct pb_manager *pmgr,
314 pb_size size,
315 const struct pb_desc *pb_desc)
316 {
317 struct d3d12_bufmgr *mgr = d3d12_bufmgr(pmgr);
318 struct d3d12_buffer *buf;
319
320 buf = CALLOC_STRUCT(d3d12_buffer);
321 if (!buf)
322 return NULL;
323
324 // Align the buffer to D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT
325 // in case it is to be used as a CBV.
326 size = align64(size, D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
327
328 pipe_reference_init(&buf->base.reference, 1);
329 buf->base.alignment_log2 = util_logbase2(pb_desc->alignment);
330 buf->base.usage = pb_desc->usage;
331 buf->base.vtbl = &d3d12_buffer_vtbl;
332 buf->base.size = size;
333 buf->range.Begin = 0;
334 buf->range.End = size;
335
336 buf->bo = d3d12_bo_new(mgr->screen, size, pb_desc);
337 if (!buf->bo) {
338 FREE(buf);
339 return NULL;
340 }
341
342 if (pb_desc->usage & PB_USAGE_CPU_READ_WRITE) {
343 buf->map = d3d12_bo_map(buf->bo, &buf->range);
344 if (!buf->map) {
345 d3d12_bo_unreference(buf->bo);
346 FREE(buf);
347 return NULL;
348 }
349 }
350
351 return &buf->base;
352 }
353
354 static void
d3d12_bufmgr_flush(struct pb_manager * mgr)355 d3d12_bufmgr_flush(struct pb_manager *mgr)
356 {
357 /* No-op */
358 }
359
360 static void
d3d12_bufmgr_destroy(struct pb_manager * _mgr)361 d3d12_bufmgr_destroy(struct pb_manager *_mgr)
362 {
363 struct d3d12_bufmgr *mgr = d3d12_bufmgr(_mgr);
364 FREE(mgr);
365 }
366
367 static boolean
d3d12_bufmgr_is_buffer_busy(struct pb_manager * _mgr,struct pb_buffer * _buf)368 d3d12_bufmgr_is_buffer_busy(struct pb_manager *_mgr, struct pb_buffer *_buf)
369 {
370 /* We're only asked this on buffers that are known not busy */
371 return false;
372 }
373
374 struct pb_manager *
d3d12_bufmgr_create(struct d3d12_screen * screen)375 d3d12_bufmgr_create(struct d3d12_screen *screen)
376 {
377 struct d3d12_bufmgr *mgr;
378
379 mgr = CALLOC_STRUCT(d3d12_bufmgr);
380 if (!mgr)
381 return NULL;
382
383 mgr->base.destroy = d3d12_bufmgr_destroy;
384 mgr->base.create_buffer = d3d12_bufmgr_create_buffer;
385 mgr->base.flush = d3d12_bufmgr_flush;
386 mgr->base.is_buffer_busy = d3d12_bufmgr_is_buffer_busy;
387
388 mgr->screen = screen;
389
390 return &mgr->base;
391 }
392