1 #ifndef NVFX_RESOURCE_H
2 #define NVFX_RESOURCE_H
4 #include "util/u_transfer.h"
5 #include "util/u_format.h"
6 #include "util/u_math.h"
7 #include "util/u_double_list.h"
8 #include "util/u_surfaces.h"
9 #include "util/u_dirty_surfaces.h"
10 #include <nouveau/nouveau_bo.h>
15 struct nvfx_resource
{
16 struct pipe_resource base
;
17 struct nouveau_bo
*bo
;
21 struct nvfx_resource
*nvfx_resource(struct pipe_resource
*resource
)
23 return (struct nvfx_resource
*)resource
;
26 #define NVFX_RESOURCE_FLAG_LINEAR (PIPE_RESOURCE_FLAG_DRV_PRIV << 0)
27 #define NVFX_RESOURCE_FLAG_USER (PIPE_RESOURCE_FLAG_DRV_PRIV << 1)
29 /* is resource mapped into the GPU's address space (i.e. VRAM or GART) ? */
31 nvfx_resource_mapped_by_gpu(struct pipe_resource
*resource
)
33 return nvfx_resource(resource
)->bo
->handle
;
36 /* is resource in VRAM? */
38 nvfx_resource_on_gpu(struct pipe_resource
* pr
)
41 // a compiler error here means you need to apply libdrm-nouveau-add-domain.patch to libdrm
42 // TODO: return FALSE if not VRAM and on a PCI-E system
43 return ((struct nvfx_resource
*)pr
)->bo
->domain
& (NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
);
49 #define NVFX_MAX_TEXTURE_LEVELS 16
51 /* We have the following invariants for render temporaries
53 * 1. Render temporaries are always linear
54 * 2. Render temporaries are always up to date
55 * 3. Currently, render temporaries are destroyed when the resource is used for sampling, but kept for any other use
57 * Also, we do NOT flush temporaries on any pipe->flush().
58 * This is fine, as long as scanout targets and shared resources never need temps.
60 * TODO: we may want to also support swizzled temporaries to improve performance in some cases.
64 struct nvfx_resource base
;
66 unsigned linear_pitch
; /* for linear textures, 0 for swizzled and compressed textures with level-dependent minimal pitch */
67 unsigned face_size
; /* 128-byte aligned face/total size */
68 unsigned level_offset
[NVFX_MAX_TEXTURE_LEVELS
];
70 struct util_surfaces surfaces
;
71 struct util_dirty_surfaces dirty_surfaces
;
75 struct util_dirty_surface base
;
79 struct nvfx_miptree
* temp
;
82 static INLINE
struct nouveau_bo
*
83 nvfx_surface_buffer(struct pipe_surface
*surf
)
85 struct nvfx_resource
*mt
= nvfx_resource(surf
->texture
);
90 static INLINE
struct util_dirty_surfaces
*
91 nvfx_surface_get_dirty_surfaces(struct pipe_surface
* surf
)
93 struct nvfx_miptree
*mt
= (struct nvfx_miptree
*)surf
->texture
;
94 return &mt
->dirty_surfaces
;
98 nvfx_init_resource_functions(struct pipe_context
*pipe
);
101 nvfx_screen_init_resource_functions(struct pipe_screen
*pscreen
);
107 struct pipe_resource
*
108 nvfx_miptree_create(struct pipe_screen
*pscreen
, const struct pipe_resource
*pt
);
111 nvfx_miptree_destroy(struct pipe_screen
*pscreen
,
112 struct pipe_resource
*presource
);
114 struct pipe_resource
*
115 nvfx_miptree_from_handle(struct pipe_screen
*pscreen
,
116 const struct pipe_resource
*template,
117 struct winsys_handle
*whandle
);
120 nvfx_miptree_surface_del(struct pipe_context
*pipe
, struct pipe_surface
*ps
);
122 struct pipe_surface
*
123 nvfx_miptree_surface_new(struct pipe_context
*pipe
, struct pipe_resource
*pt
,
124 const struct pipe_surface
*surf_tmpl
);
126 /* only for miptrees, don't use for buffers */
128 /* NOTE: for swizzled 3D textures, this just returns the offset of the mipmap level */
129 static inline unsigned
130 nvfx_subresource_offset(struct pipe_resource
* pt
, unsigned face
, unsigned level
, unsigned zslice
)
132 if(pt
->target
== PIPE_BUFFER
)
136 struct nvfx_miptree
*mt
= (struct nvfx_miptree
*)pt
;
138 unsigned offset
= mt
->level_offset
[level
];
139 if (pt
->target
== PIPE_TEXTURE_CUBE
)
140 offset
+= mt
->face_size
* face
;
141 else if (pt
->target
== PIPE_TEXTURE_3D
&& mt
->linear_pitch
)
142 offset
+= zslice
* util_format_get_2d_size(pt
->format
, (mt
->linear_pitch
? mt
->linear_pitch
: util_format_get_stride(pt
->format
, u_minify(pt
->width0
, level
))), u_minify(pt
->height0
, level
));
147 static inline unsigned
148 nvfx_subresource_pitch(struct pipe_resource
* pt
, unsigned level
)
150 if(pt
->target
== PIPE_BUFFER
)
151 return ((struct nvfx_resource
*)pt
)->bo
->size
;
154 struct nvfx_miptree
*mt
= (struct nvfx_miptree
*)pt
;
157 return mt
->linear_pitch
;
159 return util_format_get_stride(pt
->format
, u_minify(pt
->width0
, level
));
164 nvfx_surface_create_temp(struct pipe_context
* pipe
, struct pipe_surface
* surf
);
167 nvfx_surface_flush(struct pipe_context
* pipe
, struct pipe_surface
* surf
);
171 struct nvfx_resource base
;
175 /* the range of data not yet uploaded to the GPU bo */
176 unsigned dirty_begin
;
179 /* whether all transfers were unsynchronized */
180 boolean dirty_unsynchronized
;
182 /* whether it would have been profitable to upload
183 * the latest updated data to the GPU immediately */
184 boolean last_update_static
;
186 /* how many bytes we need to draw before we deem
187 * the buffer to be static
189 long long bytes_to_draw_until_static
;
192 static inline struct nvfx_buffer
* nvfx_buffer(struct pipe_resource
* pr
)
194 return (struct nvfx_buffer
*)pr
;
197 /* this is an heuristic to determine whether we are better off uploading the
198 * buffer to the GPU, or just continuing pushing it on the FIFO
200 static inline boolean
nvfx_buffer_seems_static(struct nvfx_buffer
* buffer
)
202 return buffer
->last_update_static
203 || buffer
->bytes_to_draw_until_static
< 0;
206 struct pipe_resource
*
207 nvfx_buffer_create(struct pipe_screen
*pscreen
,
208 const struct pipe_resource
*template);
211 nvfx_buffer_destroy(struct pipe_screen
*pscreen
,
212 struct pipe_resource
*presource
);
214 struct pipe_resource
*
215 nvfx_user_buffer_create(struct pipe_screen
*screen
,
221 nvfx_buffer_upload(struct nvfx_buffer
* buffer
);