ilo: improve readability of ilo_image
[mesa.git] / src / gallium / drivers / ilo / ilo_resource.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2013 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "ilo_screen.h"
29 #include "ilo_resource.h"
30
31 /*
32 * From the Ivy Bridge PRM, volume 1 part 1, page 105:
33 *
34 * "In addition to restrictions on maximum height, width, and depth,
35 * surfaces are also restricted to a maximum size in bytes. This
36 * maximum is 2 GB for all products and all surface types."
37 */
38 static const size_t ilo_max_resource_size = 1u << 31;
39
40 static const char *
41 resource_get_bo_name(const struct pipe_resource *templ)
42 {
43 static const char *target_names[PIPE_MAX_TEXTURE_TYPES] = {
44 [PIPE_BUFFER] = "buf",
45 [PIPE_TEXTURE_1D] = "tex-1d",
46 [PIPE_TEXTURE_2D] = "tex-2d",
47 [PIPE_TEXTURE_3D] = "tex-3d",
48 [PIPE_TEXTURE_CUBE] = "tex-cube",
49 [PIPE_TEXTURE_RECT] = "tex-rect",
50 [PIPE_TEXTURE_1D_ARRAY] = "tex-1d-array",
51 [PIPE_TEXTURE_2D_ARRAY] = "tex-2d-array",
52 [PIPE_TEXTURE_CUBE_ARRAY] = "tex-cube-array",
53 };
54 const char *name = target_names[templ->target];
55
56 if (templ->target == PIPE_BUFFER) {
57 switch (templ->bind) {
58 case PIPE_BIND_VERTEX_BUFFER:
59 name = "buf-vb";
60 break;
61 case PIPE_BIND_INDEX_BUFFER:
62 name = "buf-ib";
63 break;
64 case PIPE_BIND_CONSTANT_BUFFER:
65 name = "buf-cb";
66 break;
67 case PIPE_BIND_STREAM_OUTPUT:
68 name = "buf-so";
69 break;
70 default:
71 break;
72 }
73 }
74
75 return name;
76 }
77
78 static bool
79 resource_get_cpu_init(const struct pipe_resource *templ)
80 {
81 return (templ->bind & (PIPE_BIND_DEPTH_STENCIL |
82 PIPE_BIND_RENDER_TARGET |
83 PIPE_BIND_STREAM_OUTPUT)) ? false : true;
84 }
85
86 static enum gen_surface_tiling
87 winsys_to_surface_tiling(enum intel_tiling_mode tiling)
88 {
89 switch (tiling) {
90 case INTEL_TILING_NONE:
91 return GEN6_TILING_NONE;
92 case INTEL_TILING_X:
93 return GEN6_TILING_X;
94 case INTEL_TILING_Y:
95 return GEN6_TILING_Y;
96 default:
97 assert(!"unknown tiling");
98 return GEN6_TILING_NONE;
99 }
100 }
101
102 static inline enum intel_tiling_mode
103 surface_to_winsys_tiling(enum gen_surface_tiling tiling)
104 {
105 switch (tiling) {
106 case GEN6_TILING_NONE:
107 return INTEL_TILING_NONE;
108 case GEN6_TILING_X:
109 return INTEL_TILING_X;
110 case GEN6_TILING_Y:
111 return INTEL_TILING_Y;
112 default:
113 assert(!"unknown tiling");
114 return GEN6_TILING_NONE;
115 }
116 }
117
118 static void
119 tex_free_slices(struct ilo_texture *tex)
120 {
121 FREE(tex->slices[0]);
122 }
123
124 static bool
125 tex_alloc_slices(struct ilo_texture *tex)
126 {
127 const struct pipe_resource *templ = &tex->base;
128 struct ilo_texture_slice *slices;
129 int depth, lv;
130
131 /* sum the depths of all levels */
132 depth = 0;
133 for (lv = 0; lv <= templ->last_level; lv++)
134 depth += u_minify(templ->depth0, lv);
135
136 /*
137 * There are (depth * tex->base.array_size) slices in total. Either depth
138 * is one (non-3D) or templ->array_size is one (non-array), but it does
139 * not matter.
140 */
141 slices = CALLOC(depth * templ->array_size, sizeof(*slices));
142 if (!slices)
143 return false;
144
145 tex->slices[0] = slices;
146
147 /* point to the respective positions in the buffer */
148 for (lv = 1; lv <= templ->last_level; lv++) {
149 tex->slices[lv] = tex->slices[lv - 1] +
150 u_minify(templ->depth0, lv - 1) * templ->array_size;
151 }
152
153 return true;
154 }
155
156 static bool
157 tex_import_handle(struct ilo_texture *tex,
158 const struct winsys_handle *handle)
159 {
160 struct ilo_screen *is = ilo_screen(tex->base.screen);
161 const char *name = resource_get_bo_name(&tex->base);
162 enum intel_tiling_mode tiling;
163 unsigned long pitch;
164 struct intel_bo *bo;
165
166 bo = intel_winsys_import_handle(is->dev.winsys, name, handle,
167 tex->image.bo_height, &tiling, &pitch);
168 if (!bo)
169 return false;
170
171 if (!ilo_image_update_for_imported_bo(&tex->image,
172 winsys_to_surface_tiling(tiling), pitch)) {
173 ilo_err("imported handle has incompatible tiling/pitch\n");
174 intel_bo_unref(bo);
175 return false;
176 }
177
178 ilo_image_set_bo(&tex->image, bo);
179 intel_bo_unref(bo);
180
181 return true;
182 }
183
184 static bool
185 tex_create_bo(struct ilo_texture *tex)
186 {
187 struct ilo_screen *is = ilo_screen(tex->base.screen);
188 const char *name = resource_get_bo_name(&tex->base);
189 const bool cpu_init = resource_get_cpu_init(&tex->base);
190 struct intel_bo *bo;
191
192 bo = intel_winsys_alloc_bo(is->dev.winsys, name,
193 tex->image.bo_stride * tex->image.bo_height, cpu_init);
194
195 /* set the tiling for transfer and export */
196 if (bo && (tex->image.tiling == GEN6_TILING_X ||
197 tex->image.tiling == GEN6_TILING_Y)) {
198 const enum intel_tiling_mode tiling =
199 surface_to_winsys_tiling(tex->image.tiling);
200
201 if (intel_bo_set_tiling(bo, tiling, tex->image.bo_stride)) {
202 intel_bo_unref(bo);
203 bo = NULL;
204 }
205 }
206 if (!bo)
207 return false;
208
209 ilo_image_set_bo(&tex->image, bo);
210 intel_bo_unref(bo);
211
212 return true;
213 }
214
215 static bool
216 tex_create_separate_stencil(struct ilo_texture *tex)
217 {
218 struct pipe_resource templ = tex->base;
219 struct pipe_resource *s8;
220
221 /*
222 * Unless PIPE_BIND_DEPTH_STENCIL is set, the resource may have other
223 * tilings. But that should be fine since it will never be bound as the
224 * stencil buffer, and our transfer code can handle all tilings.
225 */
226 templ.format = PIPE_FORMAT_S8_UINT;
227
228 /* no stencil texturing */
229 templ.bind &= ~PIPE_BIND_SAMPLER_VIEW;
230
231 s8 = tex->base.screen->resource_create(tex->base.screen, &templ);
232 if (!s8)
233 return false;
234
235 tex->separate_s8 = ilo_texture(s8);
236
237 assert(tex->separate_s8->image.format == PIPE_FORMAT_S8_UINT);
238
239 return true;
240 }
241
242 static bool
243 tex_create_hiz(struct ilo_texture *tex)
244 {
245 const struct pipe_resource *templ = &tex->base;
246 struct ilo_screen *is = ilo_screen(tex->base.screen);
247 struct intel_bo *bo;
248 unsigned lv;
249
250 bo = intel_winsys_alloc_bo(is->dev.winsys, "hiz texture",
251 tex->image.aux.bo_stride * tex->image.aux.bo_height, false);
252 if (!bo)
253 return false;
254
255 ilo_image_set_aux_bo(&tex->image, bo);
256
257 for (lv = 0; lv <= templ->last_level; lv++) {
258 if (tex->image.aux.enables & (1 << lv)) {
259 const unsigned num_slices = (templ->target == PIPE_TEXTURE_3D) ?
260 u_minify(templ->depth0, lv) : templ->array_size;
261 unsigned flags = ILO_TEXTURE_HIZ;
262
263 /* this will trigger a HiZ resolve */
264 if (tex->imported)
265 flags |= ILO_TEXTURE_CPU_WRITE;
266
267 ilo_texture_set_slice_flags(tex, lv, 0, num_slices, flags, flags);
268 }
269 }
270
271 return true;
272 }
273
274 static bool
275 tex_create_mcs(struct ilo_texture *tex)
276 {
277 struct ilo_screen *is = ilo_screen(tex->base.screen);
278 struct intel_bo *bo;
279
280 assert(tex->image.aux.enables == (1 << (tex->base.last_level + 1)) - 1);
281
282 bo = intel_winsys_alloc_bo(is->dev.winsys, "mcs texture",
283 tex->image.aux.bo_stride * tex->image.aux.bo_height, false);
284 if (!bo)
285 return false;
286
287 ilo_image_set_aux_bo(&tex->image, bo);
288
289 return true;
290 }
291
292 static void
293 tex_destroy(struct ilo_texture *tex)
294 {
295 if (tex->separate_s8)
296 tex_destroy(tex->separate_s8);
297
298 ilo_image_cleanup(&tex->image);
299
300 tex_free_slices(tex);
301 FREE(tex);
302 }
303
304 static bool
305 tex_alloc_bos(struct ilo_texture *tex,
306 const struct winsys_handle *handle)
307 {
308 struct ilo_screen *is = ilo_screen(tex->base.screen);
309
310 if (handle) {
311 if (!tex_import_handle(tex, handle))
312 return false;
313 } else {
314 if (!tex_create_bo(tex))
315 return false;
316 }
317
318 /* allocate separate stencil resource */
319 if (tex->image.separate_stencil && !tex_create_separate_stencil(tex))
320 return false;
321
322 switch (tex->image.aux.type) {
323 case ILO_IMAGE_AUX_HIZ:
324 if (!tex_create_hiz(tex)) {
325 /* Separate Stencil Buffer requires HiZ to be enabled */
326 if (ilo_dev_gen(&is->dev) == ILO_GEN(6) &&
327 tex->image.separate_stencil)
328 return false;
329 }
330 break;
331 case ILO_IMAGE_AUX_MCS:
332 if (!tex_create_mcs(tex))
333 return false;
334 break;
335 default:
336 break;
337 }
338
339 return true;
340 }
341
342 static bool
343 tex_init_image(struct ilo_texture *tex)
344 {
345 struct ilo_screen *is = ilo_screen(tex->base.screen);
346 const struct pipe_resource *templ = &tex->base;
347 struct ilo_image *img = &tex->image;
348
349 ilo_image_init(img, &is->dev, templ);
350
351 if (img->bo_height > ilo_max_resource_size / img->bo_stride)
352 return false;
353
354 if (templ->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) {
355 /* require on-the-fly tiling/untiling or format conversion */
356 if (img->tiling == GEN8_TILING_W || img->separate_stencil ||
357 img->format != templ->format)
358 return false;
359 }
360
361 if (!tex_alloc_slices(tex))
362 return false;
363
364 return true;
365 }
366
367 static struct pipe_resource *
368 tex_create(struct pipe_screen *screen,
369 const struct pipe_resource *templ,
370 const struct winsys_handle *handle)
371 {
372 struct ilo_texture *tex;
373
374 tex = CALLOC_STRUCT(ilo_texture);
375 if (!tex)
376 return NULL;
377
378 tex->base = *templ;
379 tex->base.screen = screen;
380 pipe_reference_init(&tex->base.reference, 1);
381
382 tex->imported = (handle != NULL);
383
384 if (!tex_init_image(tex)) {
385 FREE(tex);
386 return NULL;
387 }
388
389 if (!tex_alloc_bos(tex, handle)) {
390 tex_destroy(tex);
391 return NULL;
392 }
393
394 return &tex->base;
395 }
396
397 static bool
398 tex_get_handle(struct ilo_texture *tex, struct winsys_handle *handle)
399 {
400 struct ilo_screen *is = ilo_screen(tex->base.screen);
401 enum intel_tiling_mode tiling;
402 int err;
403
404 /* must match what tex_create_bo() sets */
405 if (tex->image.tiling == GEN8_TILING_W)
406 tiling = INTEL_TILING_NONE;
407 else
408 tiling = surface_to_winsys_tiling(tex->image.tiling);
409
410 err = intel_winsys_export_handle(is->dev.winsys, tex->image.bo, tiling,
411 tex->image.bo_stride, tex->image.bo_height, handle);
412
413 return !err;
414 }
415
416 static bool
417 buf_create_bo(struct ilo_buffer_resource *buf)
418 {
419 struct ilo_screen *is = ilo_screen(buf->base.screen);
420 const char *name = resource_get_bo_name(&buf->base);
421 const bool cpu_init = resource_get_cpu_init(&buf->base);
422 struct intel_bo *bo;
423
424 bo = intel_winsys_alloc_bo(is->dev.winsys, name,
425 buf->buffer.bo_size, cpu_init);
426 if (!bo)
427 return false;
428
429 ilo_buffer_set_bo(&buf->buffer, bo);
430 intel_bo_unref(bo);
431
432 return true;
433 }
434
435 static void
436 buf_destroy(struct ilo_buffer_resource *buf)
437 {
438 ilo_buffer_cleanup(&buf->buffer);
439 FREE(buf);
440 }
441
442 static struct pipe_resource *
443 buf_create(struct pipe_screen *screen, const struct pipe_resource *templ)
444 {
445 const struct ilo_screen *is = ilo_screen(screen);
446 struct ilo_buffer_resource *buf;
447
448 buf = CALLOC_STRUCT(ilo_buffer_resource);
449 if (!buf)
450 return NULL;
451
452 buf->base = *templ;
453 buf->base.screen = screen;
454 pipe_reference_init(&buf->base.reference, 1);
455
456 ilo_buffer_init(&buf->buffer, &is->dev,
457 templ->width0, templ->bind, templ->flags);
458
459 if (buf->buffer.bo_size < templ->width0 ||
460 buf->buffer.bo_size > ilo_max_resource_size ||
461 !buf_create_bo(buf)) {
462 FREE(buf);
463 return NULL;
464 }
465
466 return &buf->base;
467 }
468
469 static boolean
470 ilo_can_create_resource(struct pipe_screen *screen,
471 const struct pipe_resource *templ)
472 {
473 struct ilo_image img;
474
475 if (templ->target == PIPE_BUFFER)
476 return (templ->width0 <= ilo_max_resource_size);
477
478 memset(&img, 0, sizeof(img));
479 ilo_image_init(&img, &ilo_screen(screen)->dev, templ);
480
481 return (img.bo_height <= ilo_max_resource_size / img.bo_stride);
482 }
483
484 static struct pipe_resource *
485 ilo_resource_create(struct pipe_screen *screen,
486 const struct pipe_resource *templ)
487 {
488 if (templ->target == PIPE_BUFFER)
489 return buf_create(screen, templ);
490 else
491 return tex_create(screen, templ, NULL);
492 }
493
494 static struct pipe_resource *
495 ilo_resource_from_handle(struct pipe_screen *screen,
496 const struct pipe_resource *templ,
497 struct winsys_handle *handle)
498 {
499 if (templ->target == PIPE_BUFFER)
500 return NULL;
501 else
502 return tex_create(screen, templ, handle);
503 }
504
505 static boolean
506 ilo_resource_get_handle(struct pipe_screen *screen,
507 struct pipe_resource *res,
508 struct winsys_handle *handle)
509 {
510 if (res->target == PIPE_BUFFER)
511 return false;
512 else
513 return tex_get_handle(ilo_texture(res), handle);
514
515 }
516
517 static void
518 ilo_resource_destroy(struct pipe_screen *screen,
519 struct pipe_resource *res)
520 {
521 if (res->target == PIPE_BUFFER)
522 buf_destroy((struct ilo_buffer_resource *) res);
523 else
524 tex_destroy(ilo_texture(res));
525 }
526
527 /**
528 * Initialize resource-related functions.
529 */
530 void
531 ilo_init_resource_functions(struct ilo_screen *is)
532 {
533 is->base.can_create_resource = ilo_can_create_resource;
534 is->base.resource_create = ilo_resource_create;
535 is->base.resource_from_handle = ilo_resource_from_handle;
536 is->base.resource_get_handle = ilo_resource_get_handle;
537 is->base.resource_destroy = ilo_resource_destroy;
538 }
539
540 bool
541 ilo_resource_rename_bo(struct pipe_resource *res)
542 {
543 if (res->target == PIPE_BUFFER) {
544 return buf_create_bo((struct ilo_buffer_resource *) res);
545 } else {
546 struct ilo_texture *tex = ilo_texture(res);
547
548 /* an imported texture cannot be renamed */
549 if (tex->imported)
550 return false;
551
552 return tex_create_bo(tex);
553 }
554 }