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
27struct 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
35enum 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
43struct 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   struct sw_displaytarget *dt;
52   unsigned dt_stride;
53   struct util_range valid_buffer_range;
54   uint32_t bind_counts[PIPE_SHADER_TYPES][D3D12_RESOURCE_BINDING_TYPES];
55   unsigned generation_id;
56};
57
58struct d3d12_memory_object {
59   struct pipe_memory_object base;
60   ID3D12Resource *res;
61   ID3D12Heap *heap;
62};
63
64struct d3d12_transfer {
65   struct threaded_transfer base;
66   struct pipe_resource *staging_res;
67   void *data;
68   unsigned zs_cpu_copy_stride;
69   unsigned zs_cpu_copy_layer_stride;
70};
71
72static inline struct d3d12_resource *
73d3d12_resource(struct pipe_resource *r)
74{
75   return (struct d3d12_resource *)r;
76}
77
78static inline struct d3d12_memory_object *
79d3d12_memory_object(struct pipe_memory_object *m)
80{
81   return (struct d3d12_memory_object *)m;
82}
83
84/* Returns the underlying ID3D12Resource and offset for this resource */
85static inline ID3D12Resource *
86d3d12_resource_underlying(struct d3d12_resource *res, uint64_t *offset)
87{
88   if (!res->bo)
89      return NULL;
90
91   return d3d12_bo_get_base(res->bo, offset)->res;
92}
93
94/* Returns the underlying ID3D12Resource for this resource. */
95static inline ID3D12Resource *
96d3d12_resource_resource(struct d3d12_resource *res)
97{
98   ID3D12Resource *ret;
99   uint64_t offset;
100   ret = d3d12_resource_underlying(res, &offset);
101   return ret;
102}
103
104static inline D3D12_GPU_VIRTUAL_ADDRESS
105d3d12_resource_gpu_virtual_address(struct d3d12_resource *res)
106{
107   uint64_t offset;
108   ID3D12Resource *base_res = d3d12_resource_underlying(res, &offset);
109   return base_res->GetGPUVirtualAddress() + offset;
110}
111
112static inline bool
113d3d12_subresource_id_uses_layer(enum pipe_texture_target target)
114{
115   return target == PIPE_TEXTURE_CUBE ||
116          target == PIPE_TEXTURE_CUBE_ARRAY ||
117          target == PIPE_TEXTURE_1D_ARRAY ||
118          target == PIPE_TEXTURE_2D_ARRAY;
119}
120
121void
122d3d12_resource_release(struct d3d12_resource *res);
123
124void
125d3d12_resource_wait_idle(struct d3d12_context *ctx,
126                         struct d3d12_resource *res,
127                         bool want_to_write);
128
129void
130d3d12_screen_resource_init(struct pipe_screen *pscreen);
131
132void
133d3d12_context_resource_init(struct pipe_context *pctx);
134
135struct pipe_resource *
136d3d12_resource_from_resource(struct pipe_screen *pscreen,
137                              ID3D12Resource* inputRes);
138
139#endif
140