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#include "util/list.h"
30
31#include "d3d12_common.h"
32#include "d3d12_resource_state.h"
33
34struct d3d12_bufmgr;
35struct d3d12_screen;
36struct pb_manager;
37
38enum d3d12_residency_status {
39   d3d12_evicted,
40   d3d12_resident,
41   d3d12_permanently_resident,
42};
43
44struct d3d12_bo {
45   struct pipe_reference reference;
46   struct d3d12_screen *screen;
47   ID3D12Resource *res;
48   struct pb_buffer *buffer;
49   struct d3d12_resource_state global_state;
50
51   /* Used as a key in per-context resource state maps,
52    * to avoid needing to lock them for single-threaded lookups to
53    * protect against resource destruction.
54    */
55   uint64_t unique_id;
56
57   struct list_head residency_list_entry;
58   uint64_t estimated_size;
59   int64_t last_used_timestamp;
60   uint64_t last_used_fence;
61   enum d3d12_residency_status residency_status;
62};
63
64struct d3d12_buffer {
65   struct pb_buffer base;
66
67   struct d3d12_bo *bo;
68   D3D12_RANGE range;
69   void *map;
70};
71
72static inline struct d3d12_buffer *
73d3d12_buffer(struct pb_buffer *buf)
74{
75   assert(buf);
76   return (struct d3d12_buffer *)buf;
77}
78
79static inline struct d3d12_bo *
80d3d12_bo_get_base(struct d3d12_bo *bo, uint64_t *offset)
81{
82   if (bo->buffer) {
83      struct pb_buffer *base_buffer;
84      pb_get_base_buffer(bo->buffer, &base_buffer, offset);
85      return d3d12_buffer(base_buffer)->bo;
86   } else {
87      *offset = 0;
88      return bo;
89   }
90}
91
92static inline uint64_t
93d3d12_bo_get_size(struct d3d12_bo *bo)
94{
95   if (bo->buffer)
96      return bo->buffer->size;
97   else
98      return GetDesc(bo->res).Width;
99}
100
101static inline bool
102d3d12_bo_is_suballocated(struct d3d12_bo *bo)
103{
104   struct d3d12_bo *base_bo;
105   uint64_t offset;
106
107   if (!bo->buffer)
108      return false;
109
110   base_bo = d3d12_bo_get_base(bo, &offset);
111   return d3d12_bo_get_size(base_bo) != d3d12_bo_get_size(bo);
112}
113
114struct d3d12_bo *
115d3d12_bo_new(struct d3d12_screen *screen, uint64_t size, uint64_t alignment);
116
117struct d3d12_bo *
118d3d12_bo_wrap_res(struct d3d12_screen *screen, ID3D12Resource *res, enum d3d12_residency_status residency);
119
120struct d3d12_bo *
121d3d12_bo_wrap_buffer(struct d3d12_screen *screen, struct pb_buffer *buf);
122
123void
124d3d12_debug_describe_bo(char* buf, struct d3d12_bo* ptr);
125
126static inline void
127d3d12_bo_reference(struct d3d12_bo *bo)
128{
129   pipe_reference_described(NULL, &bo->reference,
130                            (debug_reference_descriptor)
131                            d3d12_debug_describe_bo);
132}
133
134void
135d3d12_bo_unreference(struct d3d12_bo *bo);
136
137void *
138d3d12_bo_map(struct d3d12_bo *bo, D3D12_RANGE *range);
139
140void
141d3d12_bo_unmap(struct d3d12_bo *bo, D3D12_RANGE *range);
142
143struct pb_manager *
144d3d12_bufmgr_create(struct d3d12_screen *screen);
145
146#endif
147