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 #ifndef D3D12_BUFMGR_H
25 #define D3D12_BUFMGR_H
26
27 #include "pipebuffer/pb_buffer.h"
28 #include "util/u_atomic.h"
29
30 #ifndef _WIN32
31 #include <wsl/winadapter.h>
32 #endif
33
34 #include <directx/d3d12.h>
35
36 struct d3d12_bufmgr;
37 struct d3d12_screen;
38 struct pb_manager;
39 struct TransitionableResourceState;
40
41 struct d3d12_bo {
42 int refcount;
43 ID3D12Resource *res;
44 struct pb_buffer *buffer;
45 struct TransitionableResourceState *trans_state;
46 };
47
48 struct d3d12_buffer {
49 struct pb_buffer base;
50
51 struct d3d12_bo *bo;
52 D3D12_RANGE range;
53 void *map;
54 };
55
56 static inline struct d3d12_buffer *
d3d12_buffer(struct pb_buffer * buf)57 d3d12_buffer(struct pb_buffer *buf)
58 {
59 assert(buf);
60 return (struct d3d12_buffer *)buf;
61 }
62
63 static inline struct d3d12_bo *
d3d12_bo_get_base(struct d3d12_bo * bo,uint64_t * offset)64 d3d12_bo_get_base(struct d3d12_bo *bo, uint64_t *offset)
65 {
66 if (bo->buffer) {
67 struct pb_buffer *base_buffer;
68 pb_get_base_buffer(bo->buffer, &base_buffer, offset);
69 return d3d12_buffer(base_buffer)->bo;
70 } else {
71 *offset = 0;
72 return bo;
73 }
74 }
75
76 static inline uint64_t
d3d12_bo_get_size(struct d3d12_bo * bo)77 d3d12_bo_get_size(struct d3d12_bo *bo)
78 {
79 if (bo->buffer)
80 return bo->buffer->size;
81 else
82 return bo->res->GetDesc().Width;
83 }
84
85 static inline bool
d3d12_bo_is_suballocated(struct d3d12_bo * bo)86 d3d12_bo_is_suballocated(struct d3d12_bo *bo)
87 {
88 struct d3d12_bo *base_bo;
89 uint64_t offset;
90
91 if (!bo->buffer)
92 return false;
93
94 base_bo = d3d12_bo_get_base(bo, &offset);
95 return d3d12_bo_get_size(base_bo) != d3d12_bo_get_size(bo);
96 }
97
98 struct d3d12_bo *
99 d3d12_bo_new(ID3D12Device *dev, uint64_t size, uint64_t alignment);
100
101 struct d3d12_bo *
102 d3d12_bo_wrap_res(ID3D12Resource *res, enum pipe_format format);
103
104 struct d3d12_bo *
105 d3d12_bo_wrap_buffer(struct pb_buffer *buf);
106
107 static inline void
d3d12_bo_reference(struct d3d12_bo * bo)108 d3d12_bo_reference(struct d3d12_bo *bo)
109 {
110 p_atomic_inc(&bo->refcount);
111 }
112
113 void
114 d3d12_bo_unreference(struct d3d12_bo *bo);
115
116 void *
117 d3d12_bo_map(struct d3d12_bo *bo, D3D12_RANGE *range);
118
119 void
120 d3d12_bo_unmap(struct d3d12_bo *bo, D3D12_RANGE *range);
121
122 struct pb_manager *
123 d3d12_bufmgr_create(struct d3d12_screen *screen);
124
125 #endif
126