xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_resource.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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_RESOURCE_H
25 #define D3D12_RESOURCE_H
26 
27 struct pipe_screen;
28 #include "d3d12_bufmgr.h"
29 #include "util/u_range.h"
30 #include "util/u_transfer.h"
31 #include "util/u_threaded_context.h"
32 
33 #include "d3d12_common.h"
34 
35 enum d3d12_resource_binding_type {
36    D3D12_RESOURCE_BINDING_TYPE_SRV,
37    D3D12_RESOURCE_BINDING_TYPE_CBV,
38    D3D12_RESOURCE_BINDING_TYPE_SSBO,
39    D3D12_RESOURCE_BINDING_TYPE_IMAGE,
40    D3D12_RESOURCE_BINDING_TYPES
41 };
42 
43 struct d3d12_resource {
44    struct threaded_resource base;
45    struct d3d12_bo *bo;
46    DXGI_FORMAT dxgi_format;
47    enum pipe_format overall_format;
48    unsigned int plane_slice;
49    struct pipe_resource* first_plane;
50    unsigned mip_levels;
51 
52    struct sw_displaytarget *dt;
53    unsigned dt_refcount; /* For planar resources sharing the dt pointer */
54    unsigned dt_stride;
55    struct pipe_resource *dt_proxy;
56 
57    struct util_range valid_buffer_range;
58    uint32_t bind_counts[PIPE_SHADER_TYPES][D3D12_RESOURCE_BINDING_TYPES];
59    unsigned generation_id;
60 };
61 
62 struct d3d12_memory_object {
63    struct pipe_memory_object base;
64    ID3D12Resource *res;
65    ID3D12Heap *heap;
66 };
67 
68 struct d3d12_transfer {
69    struct threaded_transfer base;
70    struct pipe_resource *staging_res;
71    void *data;
72    unsigned zs_cpu_copy_stride;
73    unsigned zs_cpu_copy_layer_stride;
74 };
75 
76 static inline struct d3d12_resource *
d3d12_resource(struct pipe_resource * r)77 d3d12_resource(struct pipe_resource *r)
78 {
79    return (struct d3d12_resource *)r;
80 }
81 
82 static inline struct d3d12_memory_object *
d3d12_memory_object(struct pipe_memory_object * m)83 d3d12_memory_object(struct pipe_memory_object *m)
84 {
85    return (struct d3d12_memory_object *)m;
86 }
87 
88 /* Returns the underlying ID3D12Resource and offset for this resource */
89 static inline ID3D12Resource *
d3d12_resource_underlying(struct d3d12_resource * res,uint64_t * offset)90 d3d12_resource_underlying(struct d3d12_resource *res, uint64_t *offset)
91 {
92    if (!res->bo)
93       return NULL;
94 
95    return d3d12_bo_get_base(res->bo, offset)->res;
96 }
97 
98 /* Returns the underlying ID3D12Resource for this resource. */
99 static inline ID3D12Resource *
d3d12_resource_resource(struct d3d12_resource * res)100 d3d12_resource_resource(struct d3d12_resource *res)
101 {
102    ID3D12Resource *ret;
103    uint64_t offset;
104    ret = d3d12_resource_underlying(res, &offset);
105    return ret;
106 }
107 
108 static inline D3D12_GPU_VIRTUAL_ADDRESS
d3d12_resource_gpu_virtual_address(struct d3d12_resource * res)109 d3d12_resource_gpu_virtual_address(struct d3d12_resource *res)
110 {
111    uint64_t offset;
112    ID3D12Resource *base_res = d3d12_resource_underlying(res, &offset);
113    return base_res->GetGPUVirtualAddress() + offset;
114 }
115 
116 static inline bool
d3d12_subresource_id_uses_layer(enum pipe_texture_target target)117 d3d12_subresource_id_uses_layer(enum pipe_texture_target target)
118 {
119    return target == PIPE_TEXTURE_CUBE ||
120           target == PIPE_TEXTURE_CUBE_ARRAY ||
121           target == PIPE_TEXTURE_1D_ARRAY ||
122           target == PIPE_TEXTURE_2D_ARRAY;
123 }
124 
125 void
126 d3d12_resource_release(struct d3d12_resource *res);
127 
128 void
129 d3d12_resource_wait_idle(struct d3d12_context *ctx,
130                          struct d3d12_resource *res,
131                          bool want_to_write);
132 
133 void
134 d3d12_screen_resource_init(struct pipe_screen *pscreen);
135 
136 void
137 d3d12_context_resource_init(struct pipe_context *pctx);
138 
139 struct pipe_resource *
140 d3d12_resource_from_resource(struct pipe_screen *pscreen,
141                               ID3D12Resource* inputRes);
142 
143 #endif
144