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