1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © Microsoft Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef D3D12_BUFMGR_H
25bf215546Sopenharmony_ci#define D3D12_BUFMGR_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipebuffer/pb_buffer.h"
28bf215546Sopenharmony_ci#include "util/u_atomic.h"
29bf215546Sopenharmony_ci#include "util/list.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "d3d12_common.h"
32bf215546Sopenharmony_ci#include "d3d12_resource_state.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_cistruct d3d12_bufmgr;
35bf215546Sopenharmony_cistruct d3d12_screen;
36bf215546Sopenharmony_cistruct pb_manager;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cienum d3d12_residency_status {
39bf215546Sopenharmony_ci   d3d12_evicted,
40bf215546Sopenharmony_ci   d3d12_resident,
41bf215546Sopenharmony_ci   d3d12_permanently_resident,
42bf215546Sopenharmony_ci};
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistruct d3d12_bo {
45bf215546Sopenharmony_ci   struct pipe_reference reference;
46bf215546Sopenharmony_ci   struct d3d12_screen *screen;
47bf215546Sopenharmony_ci   ID3D12Resource *res;
48bf215546Sopenharmony_ci   struct pb_buffer *buffer;
49bf215546Sopenharmony_ci   struct d3d12_resource_state global_state;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   /* Used as a key in per-context resource state maps,
52bf215546Sopenharmony_ci    * to avoid needing to lock them for single-threaded lookups to
53bf215546Sopenharmony_ci    * protect against resource destruction.
54bf215546Sopenharmony_ci    */
55bf215546Sopenharmony_ci   uint64_t unique_id;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   struct list_head residency_list_entry;
58bf215546Sopenharmony_ci   uint64_t estimated_size;
59bf215546Sopenharmony_ci   int64_t last_used_timestamp;
60bf215546Sopenharmony_ci   uint64_t last_used_fence;
61bf215546Sopenharmony_ci   enum d3d12_residency_status residency_status;
62bf215546Sopenharmony_ci};
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_cistruct d3d12_buffer {
65bf215546Sopenharmony_ci   struct pb_buffer base;
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   struct d3d12_bo *bo;
68bf215546Sopenharmony_ci   D3D12_RANGE range;
69bf215546Sopenharmony_ci   void *map;
70bf215546Sopenharmony_ci};
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_cistatic inline struct d3d12_buffer *
73bf215546Sopenharmony_cid3d12_buffer(struct pb_buffer *buf)
74bf215546Sopenharmony_ci{
75bf215546Sopenharmony_ci   assert(buf);
76bf215546Sopenharmony_ci   return (struct d3d12_buffer *)buf;
77bf215546Sopenharmony_ci}
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_cistatic inline struct d3d12_bo *
80bf215546Sopenharmony_cid3d12_bo_get_base(struct d3d12_bo *bo, uint64_t *offset)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   if (bo->buffer) {
83bf215546Sopenharmony_ci      struct pb_buffer *base_buffer;
84bf215546Sopenharmony_ci      pb_get_base_buffer(bo->buffer, &base_buffer, offset);
85bf215546Sopenharmony_ci      return d3d12_buffer(base_buffer)->bo;
86bf215546Sopenharmony_ci   } else {
87bf215546Sopenharmony_ci      *offset = 0;
88bf215546Sopenharmony_ci      return bo;
89bf215546Sopenharmony_ci   }
90bf215546Sopenharmony_ci}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_cistatic inline uint64_t
93bf215546Sopenharmony_cid3d12_bo_get_size(struct d3d12_bo *bo)
94bf215546Sopenharmony_ci{
95bf215546Sopenharmony_ci   if (bo->buffer)
96bf215546Sopenharmony_ci      return bo->buffer->size;
97bf215546Sopenharmony_ci   else
98bf215546Sopenharmony_ci      return GetDesc(bo->res).Width;
99bf215546Sopenharmony_ci}
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_cistatic inline bool
102bf215546Sopenharmony_cid3d12_bo_is_suballocated(struct d3d12_bo *bo)
103bf215546Sopenharmony_ci{
104bf215546Sopenharmony_ci   struct d3d12_bo *base_bo;
105bf215546Sopenharmony_ci   uint64_t offset;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   if (!bo->buffer)
108bf215546Sopenharmony_ci      return false;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   base_bo = d3d12_bo_get_base(bo, &offset);
111bf215546Sopenharmony_ci   return d3d12_bo_get_size(base_bo) != d3d12_bo_get_size(bo);
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_cistruct d3d12_bo *
115bf215546Sopenharmony_cid3d12_bo_new(struct d3d12_screen *screen, uint64_t size, uint64_t alignment);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_cistruct d3d12_bo *
118bf215546Sopenharmony_cid3d12_bo_wrap_res(struct d3d12_screen *screen, ID3D12Resource *res, enum d3d12_residency_status residency);
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_cistruct d3d12_bo *
121bf215546Sopenharmony_cid3d12_bo_wrap_buffer(struct d3d12_screen *screen, struct pb_buffer *buf);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_civoid
124bf215546Sopenharmony_cid3d12_debug_describe_bo(char* buf, struct d3d12_bo* ptr);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_cistatic inline void
127bf215546Sopenharmony_cid3d12_bo_reference(struct d3d12_bo *bo)
128bf215546Sopenharmony_ci{
129bf215546Sopenharmony_ci   pipe_reference_described(NULL, &bo->reference,
130bf215546Sopenharmony_ci                            (debug_reference_descriptor)
131bf215546Sopenharmony_ci                            d3d12_debug_describe_bo);
132bf215546Sopenharmony_ci}
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_civoid
135bf215546Sopenharmony_cid3d12_bo_unreference(struct d3d12_bo *bo);
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_civoid *
138bf215546Sopenharmony_cid3d12_bo_map(struct d3d12_bo *bo, D3D12_RANGE *range);
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_civoid
141bf215546Sopenharmony_cid3d12_bo_unmap(struct d3d12_bo *bo, D3D12_RANGE *range);
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_cistruct pb_manager *
144bf215546Sopenharmony_cid3d12_bufmgr_create(struct d3d12_screen *screen);
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci#endif
147