d3d12_bufmgr.h revision 7ec681f3
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
36struct d3d12_bufmgr;
37struct d3d12_screen;
38struct pb_manager;
39struct TransitionableResourceState;
40
41struct d3d12_bo {
42   int refcount;
43   ID3D12Resource *res;
44   struct pb_buffer *buffer;
45   struct TransitionableResourceState *trans_state;
46};
47
48struct d3d12_buffer {
49   struct pb_buffer base;
50
51   struct d3d12_bo *bo;
52   D3D12_RANGE range;
53   void *map;
54};
55
56static inline struct d3d12_buffer *
57d3d12_buffer(struct pb_buffer *buf)
58{
59   assert(buf);
60   return (struct d3d12_buffer *)buf;
61}
62
63static inline struct d3d12_bo *
64d3d12_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
76static inline uint64_t
77d3d12_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
85static inline bool
86d3d12_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
98struct d3d12_bo *
99d3d12_bo_new(ID3D12Device *dev, uint64_t size, uint64_t alignment);
100
101struct d3d12_bo *
102d3d12_bo_wrap_res(ID3D12Resource *res, enum pipe_format format);
103
104struct d3d12_bo *
105d3d12_bo_wrap_buffer(struct pb_buffer *buf);
106
107static inline void
108d3d12_bo_reference(struct d3d12_bo *bo)
109{
110   p_atomic_inc(&bo->refcount);
111}
112
113void
114d3d12_bo_unreference(struct d3d12_bo *bo);
115
116void *
117d3d12_bo_map(struct d3d12_bo *bo, D3D12_RANGE *range);
118
119void
120d3d12_bo_unmap(struct d3d12_bo *bo, D3D12_RANGE *range);
121
122struct pb_manager *
123d3d12_bufmgr_create(struct d3d12_screen *screen);
124
125#endif
126